android ble蓝牙通讯问题
lfqsy 2017-01-03 11:01:04 我现在蓝牙可以连接了,但是一直无法获取BLE返回的数据
收索没问题,提示连接成功可以发现服务了;
触发BLE发数据,回调一直不接收,始终感觉少啥,大神帮忙分析下,
官网的看了,感觉太复杂
BLE是持续在发消息的,如下代码收不到
连接:
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.i("连接",
"BluetoothAdapter not initialized or unspecified address.");
return false;
}
// Previously connected device. Try to reconnect.
if (mBluetoothDeviceAddress != null
&& address.equals(mBluetoothDeviceAddress)
&& mBluetoothGatt != null) {
Log.i("连接",
"Trying to use an existing mBluetoothGatt for connection.");
if (mBluetoothGatt.connect()) {
mConnectionState = STATE_CONNECTING;
return true;
} else {
return false;
}
}
final BluetoothDevice device = mBluetoothAdapter
.getRemoteDevice(address);
if (device == null) {
Log.i("连接", "Device not found. Unable to connect.");
return false;
}
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
Log.i("连接", "Trying to create a new connection.");
mBluetoothGatt.connect();
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}
回调:
/**
* 连接状态检测
*/
BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mConnectionState = STATE_CONNECTED;
gatt.discoverServices();
Log.i("连接", "Disconnected from GATT server.");
Intent intent = new Intent(BleBouthService.blestatus);
intent.putExtra("status", "1");
sendBroadcast(intent);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
mConnectionState = STATE_DISCONNECTED;
Intent intent = new Intent(BleBouthService.blestatus);
intent.putExtra("status", "0");
sendBroadcast(intent);
Log.i("断开", "Disconnected from GATT server.");
} else {
}
}
// 发现服务的回调
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.i("服务", "成功发现服务");
List<BluetoothGattService> gattServices = mBluetoothGatt
.getServices();
for (BluetoothGattService gattService : gattServices) {
Log.i("service_uuid", "service_uuid"+ gattService.getUuid().toString());
if (gattService.getUuid().toString().equals(
BleBouthService.UUID_SERVICE_UUID)) {
Log.i("service_uuid", "服务器匹配成功");
}
List<BluetoothGattCharacteristic> characteristics = gattService
.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
Log.i("characteristic_uuid", "characteristic_uuid"
+ characteristic.getUuid().toString());
if (characteristic.getUuid().equals(
BleBouthService.UUID_READ_UUID)) {
if ((characteristic.getProperties() | (BluetoothGattCharacteristic.PROPERTY_NOTIFY)) > 0) {
characteristicread = gattService
.getCharacteristic(BleBouthService.UUID_READ_UUID);
mBluetoothGatt.setCharacteristicNotification(characteristicread, true);
mBluetoothGatt.readCharacteristic(characteristicread);
}
}
// boolean result_notify =
// RECEIVE_UUID.equals(Receive_Chara.getUuid());
if (characteristic.getUuid().equals(
BleBouthService.UUID_WRITEDATA)) {
if ((characteristic.getProperties() | (BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE)) > 0) {
characteristicwrite = gattService
.getCharacteristic(BleBouthService.UUID_READ_UUID);
mBluetoothGatt
.setCharacteristicNotification(
characteristicwrite, true);
// characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
// characteristicwrite = characteristic;
}
}
}
}
} else {
Log.i("服务", "成功发现失败");
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS)
Log.e("写", gatt.getDevice().getName() + " write successfully");
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
// broadcastUpdate(ACTION_DATA_AVAILABLE,
// characteristic);
Log.i("读",
gatt.getDevice().getName() + characteristic.getValue());
broadcastUpdate(BleBouthService.bleread, characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
// String response =
// UtilOnStr.parseBytesToHexString(characteristic.getValue());
Log.e("response", "The response is ");
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
// 获取到RSSI, RSSI 正常情况下 是 一个 负值,如 -33 ; 这个值的绝对值越小,代表设备离手机越近
// 通过mBluetoothGatt.readRemoteRssi();来获取
}
}
};