馬年伊始,剛剛上班的一個星期,公司里沒什么事兒可做,只是聽說馬上可能要做藍牙的項目。之前也做過關於軟硬件通訊之類的項目:android 串口通訊,android usb 轉串口通訊。
可是藍牙這塊還真是沒有怎么接觸過,只是以前每天論壇看博客看到過,跟網絡通訊差不多,也是用到socket諸如此類的關鍵字,做通訊函數。可是這藍牙4.0我聽說是新的藍牙協議,由TI公司做出來開發板的。沒接觸過,傷,真心傷!!!沒辦法,苦逼的程序員不都是這樣的么!
藍牙4.0為藍牙3.0的升級標准,擁有極低的運行和待機功耗。Bluetooth Low Energy。針對android系統開發關於藍牙4.0apk,首先要確定你所用的藍牙開發板,用的最多的就是TI個公司的cc2540。android系統方面,如果是用android 的sdk那么系統就要在4.3以上了,如果是三星的sdk或者moto的sdk就沒有那么的要求了,只要根據他提供的sdk說明來開發就行了。沒辦法老板死活不給三星的研發設備,所以本吊絲只能用android自帶的sdk開發了,所以本篇隨筆也只是針對Android4.3和ble的了。
官方android4.3 BLE開發文檔:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html
官方android4.3 BLE開發sample:http://developer.android.com/samples/BluetoothLeGatt/index.html
pc端調試工具:
我相信有了官方文檔的話開發起來應該不是很困難了,起碼有了找到組織的趕腳。
廢話太多了:
想要你的app能使用藍牙,那么permissions是必然的,沒有權限是干不了事情的:
1 <uses-permission android:name="android.permission.BLUETOOTH"/> 2 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
那么想要你的設備只有ble的有效那么還需要下面這句話:
1 <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
還要看你的設備是不是支持BluetoothLE設備。
// 判斷此設備是否支持藍牙4.0設備 if (!getPackageManager().hasSystemFeature( PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT) .show(); finish(); }
如果你的設備支持藍牙4.0那么下面要做的事情就是初始化一下關於要用的藍牙的參數了:
1 // Initializes Bluetooth adapter. 2 final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 3 mBluetoothAdapter = bluetoothManager.getAdapter(); 4 5 // Is adapter null? 6 if (mBluetoothAdapter == null) { 7 Toast.makeText(this, "bluetooth4.0 is not supported!", 8 Toast.LENGTH_SHORT).show(); 9 this.finish(); 10 return; 11 }
關於上面第三行代碼BluetoothAdapter mBluetoothAdapter。這個adapter可是個非常有用的東西,可以這么理解,它就是你手機的藍牙。
看來你已經成功拿到你操控的藍牙了,那么就要看看它當前是否能用。也就是要打開藍牙。我是用ToggleButton這個控件來控制。
1 tb_on_off.setOnCheckedChangeListener(new OnCheckedChangeListener() { 2 3 @Override 4 public void onCheckedChanged(CompoundButton buttonView, 5 boolean isChecked) { 6 if (isChecked) { 7 Intent enableBtIntent = new Intent( 8 BluetoothAdapter.ACTION_REQUEST_ENABLE); 9 startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 10 } else { 11 mBluetoothAdapter.disable(); 12 } 13 } 14 });
打開藍牙后,下面就要搜索外圍設備了。
1 /** 2 * @Title: scanLeDevice 3 * @Description: 搜索設備 4 * @param @param enable 5 * @return void 6 * @throws 7 */ 8 private void scanLeDevice(final boolean enable) { 9 if (enable) { 10 mHandler.postDelayed(new Runnable() { 11 12 @Override 13 public void run() { 14 mScanning = false; 15 mBluetoothAdapter.stopLeScan(mLeScanCallback); 16 } 17 }, SCAN_PERIOD); 18 19 mScanning = true; 20 mBluetoothAdapter.startLeScan(mLeScanCallback); 21 } else { 22 mScanning = false; 23 mBluetoothAdapter.stopLeScan(mLeScanCallback); 24 } 25 }
private BluetoothAdapter.LeScanCallback mLeScanCallback = new LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { runOnUiThread(new Runnable() { @Override public void run() { mDevListAdapter.addDevice(device); mDevListAdapter.notifyDataSetChanged(); } }); } };
搜索到你想要的設備后連接設備。
BluetoothGatt mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
BluetoothGatt我所理解的就是一台計算機的cup,收發處理數據都要靠它。
設備已經連接上了那么就可以了干一些事情了,比如收發數據。這里就不詳細說明了,我會把demo上傳來,僅供參考。
對了還要補充一點,就是ble,android4.3sdk中數據傳輸不是用socket這樣的關鍵字了。這個官方文檔已經說的很詳細了,所以這篇博客寫的比較粗糙。^-^!!!
demo連接:http://pan.baidu.com/s/1bn061nL