HM-10蓝牙模块 - BLE 4.0保持连接丢失

时间:2014-07-16 12:23:40

标签: android bluetooth hm-10

有没有人尝试使用HM-10蓝牙模块?

我可以使用Android设备与之配对并传递预定义的PIN码。根据UART返回,配对成功(模块返回OK + CONN - 表示已建立连接)

然而,几秒钟后(2-3),UART接收OK + LOST;表示连接丢失。此外,LED开始闪烁(通常,当连接处于活动状态时,它会一直亮着)

这是蓝牙通常还是HM-10模块的正常行为。

这是该产品的网站:http://www.jnhuamao.cn/bluetooth.asp?ID=1

2 个答案:

答案 0 :(得分:0)

你能解决这个问题吗?如果没有,我几乎可以肯定问题出在固件版本中。

如果您发送AT + VERS?在你的模块上,它会回复什么版本?

答案 1 :(得分:0)

我不确定,但是HM -10不支持rfcom。这意味着您必须使用GATT功能进行通信。 BLE的实体尽可能使用最小数据包,因此BLE不会一直保持连接并使用状态[属性]之类的东西。 因此,很少有代码行,例如,如何使用BLE:
1。

BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(DEVICE_ADDR);

这个设备的启动,就像简单的蓝牙一样,其中DEVICE_ADDR是你的BLE的MAC(如何找到你可以在google或堆栈溢出中找到的这个地址,它的琐碎)

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服务如何找到。这项服务需要&#34;属性&#34;通讯。 gattService.getUuid()几乎没有用于通信的uuids(我的模块中有4个),其中一些用于RX,一些用于TX等。&#34; 0000ffe0-0000-1000-8000-00805f9b34fb&#34;这是用于通信的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 cmd找到: 在我的固件AT + ADDR或AT + LADDR上 关于UUID的用法:不确定,但在我的情况下,我发现它与下一个AT + UUID [获取/设置系统SERVER_UUID] - &gt;响应+ UUID = 0xFFE0,AT + CHAR [获取/设置系统CHAR_UUID] - 响应+ CHAR = 0xFFE1。这就是为什么我得出的结论是UUID我必须使用fe&#34; 0000 [ffe0 /是来自AT的0xFFE0] -0000-1000-8000-00805f9b34fb&#34;

相关问题