Android HM-10蓝牙模块-BLE 4.0保持断开连接
有人试过使用HM-10蓝牙模块吗 我可以使用Android设备与之配对,并通过预定义的PIN。根据UART返回,配对成功(模块返回OK+CONN-表示已建立连接) 但是,几秒钟后(2-3),UART接收到OK+丢失;表示连接已断开。此外,LED开始闪烁(正常情况下,当连接处于活动状态时,它保持点亮状态) 这是一般蓝牙或HM-10模块的正常行为Android HM-10蓝牙模块-BLE 4.0保持断开连接,android,bluetooth,hm-10,Android,Bluetooth,Hm 10,有人试过使用HM-10蓝牙模块吗 我可以使用Android设备与之配对,并通过预定义的PIN。根据UART返回,配对成功(模块返回OK+CONN-表示已建立连接) 但是,几秒钟后(2-3),UART接收到OK+丢失;表示连接已断开。此外,LED开始闪烁(正常情况下,当连接处于活动状态时,它保持点亮状态) 这是一般蓝牙或HM-10模块的正常行为 这是该产品的网站:我不确定,但HM-10不支持rfcom。这意味着你必须使用GATT的功能进行交流。BLE的实体是使用尽可能少的数据包,所以BLE不会一直
这是该产品的网站:我不确定,但HM-10不支持rfcom。这意味着你必须使用GATT的功能进行交流。BLE的实体是使用尽可能少的数据包,所以BLE不会一直保持连接,而是使用状态[属性]之类的东西。 因此,很少有代码行,例如,如何使用BLE:
一, 这就是设备启动,就像简单的蓝牙一样,设备地址是你的BLE的MAC(如何找到这个地址,你可以在google或stack overflow中找到,这很简单)
2。
BluetoothGattService MBluetothGattService;
BluetoothGatt mBluetoothGatt=设备.connectGatt(this,false,mGattCallback);
BluetoothGattCallback mGattCallback=新的BluetoothGattCallback(){
@凌驾
连接状态更改的公共无效(蓝牙gatt gatt、int状态、int新闻状态){
if(newState==BluetoothProfile.STATE\u CONNECTED){
mBluetoothGatt.discoverServices();
}
}
@凌驾
发现服务上的公共无效(Bluetooth gatt,int状态){
如果(状态==蓝牙GATT.GATT\U成功){
List gattServices=mbluetothgatt.getServices();
用于(蓝牙gattService gattService:gattServices){
如果(“0000FF00-0000-1000-8000-00805f9b34fb”。等于(gattService.getUuid().toString())
{
mBluetoothGattService=gattService;
}
}
}否则{
Log.d(标记“OnServicesDiscovery received:+状态);
}
}
};
所以,这段代码的意思是:如果你能从这段代码中看到,我将描述GATT服务是如何找到的。“属性”通信需要此服务。gattService.getUuid()有几个用于通信的uuid(在我的模块中有4个),其中一些用于接收,一些用于发送等。“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);
BluetoothGattCharacteristic gattCharacteristic=mbluetothGattService.getCharacteristic(UUID.fromString(“0000ffe1-0000-1000-8000-00805f9b34fb”);
字符串msg=“HELLO BLE=)”;
字节b=0x00;
byte[]temp=msg.getBytes();
字节[]tx=新字节[temp.length+1];
tx[0]=b;
对于(int i=0;i
发送消息后,请稍候,您可以发送另一条消息,也可以关闭连接。
更多信息,请访问。
PS:ble扫描代码或AT cmd可以找到模块的MAC地址:
在+ADDR或+LADDR的固件上
关于UUID的用法:不确定,但在我的例子中,我在下面的AT+UUID[Get/Set system SERVER\u UUID]->Response+UUID=0xFFE0,AT+CHAR[Get/Set system CHAR\u UUID]-Response+CHAR=0xFFE1中找到了它。这就是为什么我得出结论,我必须使用fe“0000[ffe0/是来自AT响应的0xFFE0]-0000-1000-8000-00805f9b34fb”的UUID也与BLE模块有问题。我试着把它配对。插入pincode。。终端返回+连接(我认为是另一个固件)。不在已连接设备列表中。但在安卓系统中,它说配对。。。(非绑定设备:()你的安卓版本是什么?)嗨,约翰,我在用安卓4.0.4Hmm。好的,也许可以用一些有4.4的手机试试。它有一些错误修复(谷歌说)。我想我也要订购HM-10。
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);
}
}
};
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);