BluetoothLE-Multi-Library 一个能够连接多台蓝牙设备的库,它可以作为client端,也可以为server端。支持主机/从机,外围设备连接。
github地址:https://github.com/qindachang/BluetoothLE-Multi-Library
BluetoothLE-Multi-Library
一个能够连接多台蓝牙设备的库,它可以作为client端,也可以为server端。支持主机/从机,外围设备连接。
在发送消息时,它内部支持队列控制,避免因蓝牙间隔出现操作失败的情况。
开始使用
1. 主机client
扫描
BluetoothLeScannerCompat scannerCompat = BluetoothLeScannerCompat.getScanner();
ScanSettings scanSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setReportDelay(int reportDelayMillis) //0 or above >0
.setUseHardwareBatchingIfSupported(false)
.build(); //设置过滤扫描
List<ScanFilter> filters = new ArrayList<>(); ScanFilter builder = new ScanFilter.Builder().setDeviceName(deviceName).build();
filters.add(builder); ScanFilter builder = new ScanFilter.Builder().setDeviceAddress(deviceAddress).build();
filters.add(builder); ScanFilter builder = new ScanFilter.Builder()
.setServiceUuid(ParcelUuid.fromString(serviceUUID.toString())).build();
filters.add(builder); scannerCompat.startScan(filters, scanSettings, scanCallback);
扫描回调
private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(final int callbackType, final ScanResult result) {
}
@Override
public void onBatchScanResults(final List<ScanResult> results) {
}
@Override
public void onScanFailed(final int errorCode) {
}
};
连接
//创建连接的一个对象,后续将使用该对象来访问操作
private BluetoothLeConnector connector = BluetoothLe.newConnector();
private BluetoothGatt mBluetoothGatt; //配置连接对象
connector.setConfig(new BluetoothConfig.Builder()
.enableQueueInterval(true)//开启操作时间间隔
.setQueueIntervalTime(BluetoothConfig.AUTO)//单位ms,这里为自动
.build());
//连接蓝牙
connector.connect(true, mBluetoothDevice);
connector.connect(true, mBluetoothDevice, BluetoothLeConnector.TRANSPORT_AUTO);//最后一个参数设置连接通道
//开启indicate通知
connector.enableIndication(true,UUID_SERVICE,UUID_INDICATION);
//开启notify通知
connector.enableNotification(true, UUID_SERVICE, UUID_NOTIFICATION);
//写数据
connector.writeCharacteristic(new byte[]{0x01, 0x02}, UUID_SERVICE, UUID_WRITE);
//自定义写数据
BluetoothGattService service = mBluetoothGatt.getService(UUID_SERVICE);
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID_WRITE);
characteristic.setValue(byte[] value);
characteristic.setValue(int value, int formatType, int offset);
characteristic.setValue(int mantissa, int exponent, int formatType, int offset);
characteristic.setValue(String value);
connector.writeCharacteristic(characteristic);
//读数据
connector.readCharacteristic(UUID_SERVICE, UUID_READ);
//断开连接
connector.disconnect();
//关闭gatt
connector.close();
回调监听
//连接状态监听
private ConnectListener mConnectListener = new ConnectListener() {
@Override
public void connecting() { } @Override
public void connected() { } @Override
public void disconnected() { } @Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) { } @Override
public void error(ConnBleException e) { }
};
connector.addConnectListener(mConnectListener);
更多的回调监听如下:
mBleManager.addConnectListener(...)
mBleManager.addNotificationListener(...)
mBleManager.addIndicationListener(...)
mBleManager.addWriteCharacteristicListener(...)
mBleManager.addReadCharacteristicListener(...)
mBleManager.addRssiListener(...)
移除回调监听(好的程序员要懂避免内存泄漏):
connector.removeListener(mConnectListener);
2. 从机Server
广播
private GattServer mGattServer = new GattServer();
mGattServer.startAdvertising(UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb"));//该uuid可提供给主机client过滤扫描
mGattServer.stopAdvertising();
伺服器server
1. 启动startServer
mGattServer.startServer(context);
2. 关闭closeServer
mGattServer.closeServer();
3. 添加服务addService
List<ServiceProfile> list = new ArrayList<>(); //设置一个写的特征
ServiceProfile profile = new ServiceProfile();
profile.setCharacteristicUuid(UUID.fromString("0000fff3-0000-1000-8000-00805f9b34fb"));
profile.setCharacteristicProperties(BluetoothGattCharacteristic.PROPERTY_WRITE);
profile.setCharacteristicPermission(BluetoothGattCharacteristic.PERMISSION_WRITE);
profile.setDescriptorUuid(GattServer.CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
profile.setDescriptorPermission(BluetoothGattDescriptor.PERMISSION_READ);
profile.setDescriptorValue(new byte[]{0});
list.add(profile); //设置一个读的特征
ServiceProfile profile1 = new ServiceProfile();
profile1.setCharacteristicUuid(UUID.fromString("0000fff2-0000-1000-8000-00805f9b34fb"));
profile1.setCharacteristicProperties(BluetoothGattCharacteristic.PROPERTY_READ);
profile1.setCharacteristicPermission(BluetoothGattCharacteristic.PERMISSION_READ);
profile1.setDescriptorUuid(GattServer.CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
profile1.setDescriptorPermission(BluetoothGattDescriptor.PERMISSION_READ);
profile1.setDescriptorValue(new byte[]{1});
list.add(profile1); //设置一个notify通知
ServiceProfile profile2 = new ServiceProfile();
profile2.setCharacteristicUuid(UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb"));
profile2.setCharacteristicProperties(BluetoothGattCharacteristic.PROPERTY_NOTIFY);
profile2.setCharacteristicPermission(BluetoothGattCharacteristic.PERMISSION_READ);
profile2.setDescriptorUuid(GattServer.CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
profile2.setDescriptorPermission(BluetoothGattDescriptor.PERMISSION_WRITE);
profile2.setDescriptorValue(new byte[]{1});
list.add(profile2); final ServiceSettings serviceSettings = new ServiceSettings.Builder()
.setServiceUuid(UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb"))//服务uuid
.setServiceType(BluetoothGattService.SERVICE_TYPE_PRIMARY)
.addServiceProfiles(list)//上述设置添加到该服务里
.build(); mGattServer.addService(serviceSettings);
回调监听
mGattServer.setOnAdvertiseListener(new OnAdvertiseListener() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
setContentText("开启广播 成功,uuid:0000fff0-0000-1000-8000-00805f9b34fb");
}
@Override
public void onStartFailure(int errorCode) {
setContentText("开启广播 失败,uuid:0000fff0-0000-1000-8000-00805f9b34fb");
}
@Override
public void onStopAdvertising() {
setContentText("停止广播,uuid:0000fff0-0000-1000-8000-00805f9b34fb");
}
});
mGattServer.setOnServiceAddedListener(new OnServiceAddedListener() {
@Override
public void onSuccess(BluetoothGattService service) {
setContentText("添加服务成功!");
}
@Override
public void onFail(BluetoothGattService service) {
setContentText("添加服务失败");
}
});
mGattServer.setOnConnectionStateChangeListener(new OnConnectionStateChangeListener() {
@Override
public void onChange(BluetoothDevice device, int status, int newState) {
}
@Override
public void onConnected(BluetoothDevice device) {
setContentText("连接上一台设备 :{ name = " + device.getName() + ", address = " + device.getAddress() + "}");
mBluetoothDevice = device;
}
@Override
public void onDisconnected(BluetoothDevice device) {
setContentText("设备断开连接 :{ name = " + device.getName() + ", address = " + device.getAddress() + "}");
}
});
mGattServer.setOnWriteRequestListener(new OnWriteRequestListener() {
@Override
public void onCharacteristicWritten(BluetoothDevice device, BluetoothGattCharacteristic characteristic, byte[] value) {
setContentText("设备写入特征请求 : device = " + device.getAddress() + ", characteristic uuid = " + characteristic.getUuid().toString() + ", value = " + Arrays.toString(value));
}
@Override
public void onDescriptorWritten(BluetoothDevice device, BluetoothGattDescriptor descriptor, byte[] value) {
setContentText("设备写入描述请求 : device = " + device.getAddress() + ", descriptor uuid = " + descriptor.getUuid().toString() + ", value = " + Arrays.toString(value));
}
});
Download
dependencies {
compile 'will be come soon..'
}
BluetoothLE-Multi-Library 一个能够连接多台蓝牙设备的库,它可以作为client端,也可以为server端。支持主机/从机,外围设备连接。的更多相关文章
- com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
问题描述:最简单的数据库连接报错,到主机 的 TCP/IP 连接失败.(win 7 操作系统) 错误信息: com.microsoft.sqlserver.jdbc.SQLServerExcep ...
- LinkedIn的即时消息:在一台机器上支持几十万条长连接
最近我们介绍了LinkedIn的即时通信,最后提到了分型指标和读回复.为了实现这些功能,我们需要有办法通过长连接来把数据从服务器端推送到手机或网页客户端,而不是许多当代应用所采取的标准的请求-响应模式 ...
- 一步一步学Python(2) 连接多台主机执行脚本
最近在客户现场,每日都需要巡检大量主机系统的备库信息.如果一台台执行,时间浪费的就太冤枉了. 参考同事之前写的一个python脚本,配合各主机上写好的shell检查脚本,实现一次操作得到所有巡检结果. ...
- noVNC连接多台远程主机
noVNC是一个HTML5 VNC客户端,采用HTML5 websockets.Canvas和JavaScript实现,noVNC被普遍应用于各大云计算.虚拟机控制面板中,比如OpenStack Da ...
- Appium同时连接多台手机进行测试(多线程)
作为测试小白,当时遇到了N多问题: 开启多线程后,发现app启动后,用例就停止了:且启动app对应的手机不能正确对应,用例中是A手机跑A用例,结果启动了B手机跑A用例报错. 主要原因:Appium S ...
- 000 - 准备工作ADB wifi连接多台鸿蒙设备进行调试
首先将两台鸿蒙设备插入电脑的usb上 查看两台鸿蒙设备的deviceid C:\Users\Administrator>adb devices * daemon not running; sta ...
- 公布一个基于 Reactor 模式的 C++ 网络库
公布一个基于 Reactor 模式的 C++ 网络库 陈硕 (giantchen_AT_gmail) Blog.csdn.net/Solstice 2010 Aug 30 本文主要介绍 muduo 网 ...
- (转) linux虚拟机中和主机三种网络连接方式的区别
在介绍网络模式之前,关于网络的几个简单命令的使用 ifup eth0 //启动网卡eth0 ifdown eth0 //关闭网卡eth0 /etc/network/interfaces //网络 ...
- jmeter分布式测试教程和远程的代理机无法连接网络的问题解决方法
一.Jmeter分布式执行原理: 1.Jmeter分布式测试时,选择其中一台作为控制机(Controller),其它机器做为代理机(Agent). 2.执行时,Controller会把脚本发送到每台A ...
随机推荐
- HDU5618 Jam's problem again
CDQ分治模板题 #include<cstdio> #include<cctype> #include<algorithm> #include<cstring ...
- PHP微信公众平台OAuth2.0网页授权,获取用户信息代码类封装demo(二)
一.这个文件微信授权使用的是OAuth2.0授权的方式.主要有以下简略步骤: 第一步:判断有没有code,有code去第三步,没有code去第二步 第二步:用户同意授权,获取code 第三步:通过co ...
- 迅雷在P2P网络中的另类上传速度
如上图,我们一般在下载BT时,一般P2P是边下载边上传. 但是迅雷在自己的软件中可以设置上传速度,反而在展示时却把P2P协议的速度不在上传那么显示,而是使用协议速度来进行展示:并且这个速度无法设置. ...
- 【bootstrap】使用支持bootstrap的时间插件daterangepicker
其中的架包和代码,具体可以去GitHub下查看: https://github.com/AngelSXD/myagenorderdiscount 1.引入js和css <link href=&q ...
- 转:国内外著名开源b2c电子商务系统比较包括asp.net和php
from: http://longdick.iteye.com/blog/1122879 国内外著名开源b2c电子商务系统比较包括asp.net和php 博客分类: 电子商务 国内外著名开源b2c ...
- Java常见面试题汇总(一)
1)什么是Java虚拟机?为什么Java被称作是"平台无关的编程语言"? Java虚拟机是一个能够运行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机运行的字节 ...
- hibernate session缓存
Session 概述 Session 接口是 Hibernate 向应用程序提供的操纵数据库的最基本的接口, 它提供了基本的保存, 更新, 删除和载入 Java 对象的方法. Session 具有一个 ...
- 谈谈Runtime类中的freeMemory,totalMemory,maxMemory几个方法
最近在网上看到一些人讨论到java.lang.Runtime类中的freeMemory(),totalMemory(),maxMemory ()这几个方法的一些问题,很多人感到很疑惑,为什么,在jav ...
- Django+uwsgi+nginx+angular.js项目部署
这次部署的前后端分离的项目: 前端采用angular.js,后端采用Django(restframework),他俩之间主要以json数据作为交互 Django+uwsgi的配置可以参考我之前的博客: ...
- 宜人贷蜂巢API网关技术解密之Netty使用实践
一.背景 宜人贷蜂巢团队,由Michael创立于2013年,通过使用互联网科技手段助力金融生态和谐健康发展.自成立起一直致力于多维度数据闭环平台建设.目前团队规模超过百人,涵盖征信.电商.金融.社交. ...