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();来获取
}
}

};
...全文
280 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lfqsy 2017-01-11
  • 打赏
  • 举报
回复
有结果在onCharacteristicChanged里面
lfqsy 2017-01-03
  • 打赏
  • 举报
回复
先顶一下等人回复

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧