mooc课程精选,成品人片观看入口众乐乐,久久久久人妻一区精品性色av,苍兰诀大结局是什么,白丝美女被狂躁免费视频网站

當前位置:首頁 > 嵌入式培訓 > 嵌入式學習 > 講師博文 > 安卓中實現藍牙通信的工具

安卓中實現藍牙通信的工具 時間:2018-09-21      來源:未知

智能穿戴中,想要獲得數據,首先需要連接上設備的藍牙才行,下邊介紹一種連接設備的方法。

NormalText Code

// 搜索設備并添加到列表中

public Boolean SearchToList() {// 打開藍牙,搜索設備

if (bluetoothAdapter != null) {

if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {

// 打開藍牙

bluetoothAdapter.enable();

log.E("打開藍牙!");

// 搜索設備

bluetoothAdapter.startLeScan(scanCallback);

log.E("開始搜索!");

return true;

} else {

// 搜索設備

bluetoothAdapter.startLeScan(scanCallback);

log.E("開始搜索!");

return true;

}

} else {

log.E("bluetoothAdapter == null");

return false;

}

}

private BluetoothAdapter.LeScanCallback scanCallback = new BluetoothAdapter.LeScanCallback()

@Override

public void onLeScan(final BluetoothDevice device, int rssi,byte[] scanRecord) {

if ((device != null) && (deviceList != null)) {

if ((Isrepeat(device, deviceList) == false)&& (device.getName() != null)) {

deviceList.add(device);

deviceName.add(device.getName());

deviceAddr.add(device.getAddress());

}

}

}

};

連接上設備以后,還需要獲得設備的相關信息。

NormalText Code

// 連接設備并獲得特征值

public synchronized boolean BLEConnect(BluetoothDevice remoteDev) {

if (remoteDev == null) {

return false;

}

bluetoothGatt = remoteDev.connectGatt(context, false, gattCallback);

return true;

}

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {

@Override

public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {

if (bluetoothGatt != null) {

if (newState == BluetoothProfile.STATE_CONNECTED) {

log.E("連接成功!");

handler.sendEmptyMessage(3);

gatt.discoverServices();

} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

log.E("連接斷開!");

handler.sendEmptyMessage(4);

}

}

}

@Override

public void onServicesDiscovered(BluetoothGatt gatt, int status) {

if (gatt != null) {

bluetoothService = gatt.getService(UUID.fromString(serviceUUID));

if (bluetoothService != null) {

characteristic = bluetoothService.getCharacteristic(UUID.fromString(characteristicUUID));

if (characteristic != null) {

bluetoothGatt.setCharacteristicNotification(characteristic, true);

// bluetoothGatt.readCharacteristic(characteristic);

} else {

log.E("characteristic == null");

}

} else {

log.E("bluetoothService == null");

}

}

}

@Override

public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {

// log.E("onCharacteristicChanged");

readDate = characteristic.getValue();

if ((readDate != null) && (readDate.length > 0)) {

handler.sendEmptyMessage(1);

handler.sendEmptyMessage(2);

} else {

log.E("接收數據失敗!");

}

}

@Override

public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {

log.E("onCharacteristicWrite" + "-" + status);

}

@Override

public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {

log.E("onCharacteristicRead" + "-" + status);

}

}

上一篇:利用Java反射機制改變SharedPreferences存儲路徑

下一篇:解決Android關于SD卡轉化為ext4以及熱插拔支持功能

熱點文章推薦
華清學員就業榜單
高薪學員經驗分享
熱點新聞推薦
前臺專線:010-82525158 企業培訓洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠見科技集團有限公司 版權所有 ,京ICP備16055225號-5,京公海網安備11010802025203號

回到頂部