安卓蓝牙操作库,把蓝牙请求封装像Http一样

描述:

安卓蓝牙操作库,把蓝牙请求封装像Http一样,几行代码就能与物联网智能硬件设备交互

应用截图:

蓝牙

源码解析:

向远程蓝牙设备发送指令 异步发送消息,并在主线程回调。 参数含义: 设备的Mac地址, 消息对象, 是否需要获取响应信息, 监听器.

MessageItem item = new MessageItem("Hello");

btHelperClient.sendMessage("20:15:03:18:08:63", item, true, new OnSendMessageListener() {

       @Override
       public void onSuccess(int status, String response) {
          // Call when send a message succeed, and get a response from the remote device

          // status:   the status describes ok or error.
          //           1 respect the response is valid, -1 respect the response is invalid
          // response: the response from the remote device, you can call response.getBytes() to get char[]

       }

       @Override
       public void onConnectionLost(Exception e) {
           e.printStackTrace();
       }

       @Override
       public void onError(Exception e) {
           e.printStackTrace();
       }

});

搜索蓝牙设备 搜索蓝牙设备调用 searchDevices() 方法即可

btHelperClient.searchDevices(new OnSearchDeviceListener() {

       @Override
       public void onStartDiscovery() {
           // Call before discovery devices

           Log.d(TAG, "onStartDiscovery()");

       }

       @Override
       public void onNewDeviceFounded(BluetoothDevice device) {
           // Call when found a new device

           Log.d(TAG, "new device: " + device.getName() + " " + device.getAddress());

       }

       @Override
       public void onSearchCompleted(List<BluetoothDevice> bondedList, List<BluetoothDevice> newList) {
           // Call when the discovery process completed

           Log.d(TAG, "SearchCompleted: bondedList" + bondedList.toString());
           Log.d(TAG, "SearchCompleted: newList" + newList.toString());

       }

       @Override
       public void onError(Exception e) {

           e.printStackTrace();

       }

});

源码下载:https://github.com/a-voyager/BluetoothHelper

未经允许不得转载:安度博客 » 安卓蓝牙操作库,把蓝牙请求封装像Http一样

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址