有人试过使用HM-10蓝牙模块吗?
我可以使用Android设备并传递预定义的PIN与其配对。根据UART返回,配对成功(模块返回OK+CONN -表示连接已建立)
但是,几秒钟(2-3)后,UART收到OK+LOST;表示连接已丢失。此外,LED开始闪烁(正常情况下,当连接处于活动状态时,它将保持亮起)
这是蓝牙的正常行为还是HM-10模块的正常行为。
这是该产品的网站:http://www.jnhuamao.cn/bluetooth.asp?ID=1
发布于 2015-09-07 19:13:37
我不确定,但HM -10不支持rfcom。这意味着您必须使用GATT功能进行通信。BLE的实体是尽可能使用最小的数据包,所以BLE不会一直保持连接,而是使用状态属性之类的东西。所以,几行代码,例如,如何使用BLE:
1.
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(DEVICE_ADDR);这就是设备初始化,就像简单的蓝牙一样,其中DEVICE_ADDR是你的BLE的MAC地址(如何找到这个地址,你可以在谷歌或堆栈溢出中找到,它很简单)
2.
BluetoothGattService mBluetoothGattService;
BluetoothGatt mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mBluetoothGatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
List<BluetoothGattService> gattServices = mBluetoothGatt.getServices();
for(BluetoothGattService gattService : gattServices) {
if("0000ffe0-0000-1000-8000-00805f9b34fb".equals(gattService.getUuid().toString()))
{
mBluetoothGattService = gattService;
}
}
} else {
Log.d(TAG, "onServicesDiscovered received: " + status);
}
}
};那么,这段代码的含义是:如果您可以从这部分代码中看到,我将描述GATT服务如何查找。此服务是“属性”通信所需的。gattService.getUuid()有几个用于通信的uuid (在我的模块中有4个),其中一些用于RX,一些用于TX等。"0000ffe0-0000-1000-8000-00805f9b34fb“这是用于通信的uuid之一,这就是我检查它的原因。代码的最后一部分是消息发送:
BluetoothGattCharacteristic gattCharacteristic = mBluetoothGattService.getCharacteristic(UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb"));
String msg = "HELLO BLE =)";
byte b = 0x00;
byte[] temp = msg.getBytes();
byte[] tx = new byte[temp.length + 1];
tx[0] = b;
for(int i = 0; i < temp.length; i++)
tx[i+1] = temp[i];
gattCharacteristic.setValue(tx);
mBluetoothGatt.writeCharacteristic(gattCharacteristic);发送消息后,请稍候,您可以发送另一条消息或关闭连接。更多信息,你可以在https://developer.android.com/guide/topics/connectivity/bluetooth-le.html上找到。PS:你的模块的MAC地址可以用ble扫描器代码或AT :在我的固件AT+ADDR或AT+LADDR上找到关于UUID的用法:不确定,但在我的例子中,我通过next AT+UUID Get/Set system SERVER_UUID -> Response +UUID=0xFFE0,AT+CHAR Get/Set system CHAR_UUID - Response +CHAR=0xFFE1找到它。这就是为什么我得出结论,我必须使用fe“0000ffe0/的UUID是来自AT响应的0xFFE0 -0000-1000-8000-00805f9b34fb”
https://stackoverflow.com/questions/24780714
复制相似问题