Android 蓝牙4.0在实际开发中的运用
1.藍(lán)牙搜索.
?首先是獲取BluetoothAdapter對象:
? ?final BluetoothManager bluetoothManager =
??????????????? (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
????BluetoothAdapter?? bluetoothAdapter = bluetoothManager.getAdapter();
?? 當(dāng)bluetoothAdapter不為空時(shí)代表設(shè)備支持BLE4.0.
?? 然后調(diào)用BluetoothAdapter 的startLeScan(callback)方法,這里需要傳入一個(gè)callBack對象,用于搜到設(shè)備時(shí)回調(diào)。
實(shí)現(xiàn)如下:
?? ?private BluetoothAdapter.LeScanCallback callback =
??????????? new BluetoothAdapter.LeScanCallback() {
??????? @Override
??????? public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
??????????? runOnUiThread(new Runnable() {
??????????????? @Override
??????????????? public void run() {
????????????????? //如果搜到設(shè)備,將會(huì)執(zhí)行這里,device即是搜到的設(shè)備。一般這里需要將搜到的設(shè)備添加到listView的適配器中,用于顯示到界面
??????????????? }
??????????? });
??????? }
??? };
停止搜索調(diào)用BluetoothAdapter的stopLeScan(callback)方法。
2.鏈接藍(lán)牙以及接收數(shù)據(jù).
?連接藍(lán)牙代碼如下:
?BluetoothGatt bluetoothGatt = device.connectGatt(this, false, mGattCallback);
第一個(gè)參數(shù)是Context對象,第二個(gè)參數(shù)表示是否自動(dòng)連接,第三個(gè)是一個(gè)CallBack對象,當(dāng)設(shè)備連接狀態(tài)發(fā)生改變,數(shù)據(jù)的收發(fā)狀態(tài)改變時(shí)回調(diào)。
? private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
??????? @Override
??????? public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
??????????? String intentAction;
??????????? if (newState == BluetoothProfile.STATE_CONNECTED) {//連接成功
??????????? } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {//斷開連接
??????????????
??????????? }
??????? }
??????? @Override
??????? public void onServicesDiscovered(BluetoothGatt gatt, int status) {
???????
??????? }
??????? @Override
??????? public void onCharacteristicRead(BluetoothGatt gatt,
???????????????????????????????????????? BluetoothGattCharacteristic characteristic,
???????????????????????????????????????? int status) {
??????????
??????? }
??????? @Override
??????? public void onCharacteristicChanged(BluetoothGatt gatt,
??????????????????????????????????????????? BluetoothGattCharacteristic characteristic) {
? ? ? ? byte[] data = characteristic.getValue();//如果連接設(shè)備發(fā)送數(shù)據(jù)到手機(jī)端,將通過這個(gè)函數(shù)獲取數(shù)據(jù)。
??????????
??????? }
??? };
device是第一步搜到的設(shè)備BluetoothDevice對象,也可以通過獲取該設(shè)備的MAC地址,然后再獲取BluetoothDevice對象:
??????? final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceMAC);
如果bluetoothGatt不為空,則連接成功.
3.藍(lán)牙發(fā)數(shù)據(jù).
? 通過BLE發(fā)送數(shù)據(jù)時(shí),需要知道連接設(shè)備的一些服務(wù)UUID,一般UUID都類似。
public static final UUID SERVICE = UUID
?? ??? ??? ??? ?.fromString("0000FFF0-0000-1000-8000-00805F9B34FB");
public static final UUID SEND_UUID = UUID
?? ??? ??? ??? ?.fromString("0000FFF6-0000-1000-8000-00805F9B34FB");
public static final UUID RECEIVE_UUID = UUID
?? ??? ??? ??? ?.fromString("0000FFF7-0000-1000-8000-00805F9B34FB");
public static final UUID CCC = UUID
?? ??? ??? ?.fromString("00002902-0000-1000-8000-00805f9b34fb");
有了這些UUID后就可以開始發(fā)送數(shù)據(jù):
BluetoothGattService bluetoothGattService = bluetoothGatt
?? ??? ??? ??? ?.getService(SERVICE);?????????? //獲取UUID對應(yīng)的服務(wù)。
BluetoothGattCharacteristic charac = bluetoothGattService
?? ??? ??? ??? ?.getCharacteristic(SEND_UUID); //獲取指定服務(wù)下的特性
然后就是注冊一個(gè)數(shù)據(jù)接收通知
if (bluetoothGatt.setCharacteristicNotification(charac, true)) {
?? ??? ??? ?BluetoothGattDescriptor clientConfig = charac
?? ??? ??? ??? ??? ?.getDescriptor(CCC);
?? ??? ??? ?if (clientConfig != null) {
?? ??? ??? ??? ?clientConfig
?? ??? ??? ??? ??? ??? ?.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
?? ??? ??? ??? ?bluetoothGatt.writeDescriptor(clientConfig);
?? ??? ??? ?}
?? ??? ?}
??? charac.setValue(data); //這里將要發(fā)送的數(shù)據(jù)加入該特性中,data 類型為byte數(shù)組,如果數(shù)據(jù)超過20字節(jié)需要在每次發(fā)送加入間隔。
?? ??? ?try {
?? ??? ??? ?Thread.sleep(50);
?? ??? ?} catch (InterruptedException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?bluetoothGatt.writeCharacteristic(charac);//發(fā)送數(shù)據(jù)
總結(jié)
以上是生活随笔為你收集整理的Android 蓝牙4.0在实际开发中的运用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: es like模糊匹配_Elastic
- 下一篇: android webview 长按菜单