【问题标题】:HM-10 Bluetooth Module - BLE 4.0 Keep Losing ConnectionHM-10 蓝牙模块 - BLE 4.0 保持失去连接
【发布时间】:2017-07-20 19:57:39
【问题描述】:

有人试过用HM-10蓝牙模块吗?

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

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

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

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

【问题讨论】:

  • 我的 BLE 模块也有问题。我试着配对它。输入密码。终端返回+CONNECTED(我认为它是另一个固件)。比它不在列表中的连接设备。但在 android 中它说配对......(不是在有界设备中 :()
  • 你的安卓版本是多少??
  • 嗨 Johan,我使用的是 android 4.0.4
  • 嗯。好吧,也许用一些4.4的手机试试。它有一些错误修复(谷歌说)。我想我也要订购 HM-10。

标签: android bluetooth hm-10


【解决方案1】:

我不确定,但是 HM -10 不支持 rfcom。这意味着您必须使用 GATT 功能进行通信。 BLE 的实体是尽可能使用最小数据包,因此 BLE 不会一直保持连接并使用状态 [attributes] 之类的东西。 因此,例如,几行代码,如何使用 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 服务是如何找到的。 “属性”通信需要此服务。 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 cmd找到: 在我的固件 AT+ADDR 或 AT+LADDR 关于 UUID 的使用:不确定,但在我的情况下,我发现它带有下一个 AT+UUID [Get/Set system SERVER_UUID] -> Response +UUID=0xFFE0, AT+CHAR [Get/Set system CHAR_UUID] - Response +CHAR= 0xFFE1。这就是为什么我得出结论,我必须使用 fe "0000[ffe0/is 0xFFE0 from AT response]-0000-1000-8000-00805f9b34fb"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 2014-10-09
    • 2020-12-27
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    相关资源
    最近更新 更多