Flutter源码-蓝牙操作插件

Obtain an instance

FlutterBlue flutterBlue = FlutterBlue.instance;

Scan for devices

// Start scanning
flutterBlue.startScan(timeout: Duration(seconds: 4));
// Listen to scan results
var subscription = flutterBlue.scanResults.listen((results) {
    // do something with scan results
    for (ScanResult r in results) {
        print('${r.device.name} found! rssi: ${r.rssi}');
    }
});
// Stop scanning
flutterBlue.stopScan();

Connect to a device

// Connect to the device
await device.connect();
// Disconnect from device
device.disconnect();

Discover services

List<BluetoothService> services = await device.discoverServices();
services.forEach((service) {
    // do something with service
});

Read and write characteristics

// Reads all characteristics
var characteristics = service.characteristics;
for(BluetoothCharacteristic c in characteristics) {
    List<int> value = await c.read();
    print(value);
}
// Writes to a characteristic
await c.write([0x12, 0x34])

Read and write descriptors

// Reads all descriptors
var descriptors = characteristic.descriptors;
for(BluetoothDescriptor d in descriptors) {
    List<int> value = await d.read();
    print(value);
}
// Writes to a descriptor
await d.write([0x12, 0x34])

Set notifications and listen to changes

await characteristic.setNotifyValue(true);
characteristic.value.listen((value) {
    // do something with new value
});

Read the MTU and request larger size

final mtu = await device.mtu.first;
await device.requestMtu(512);

Note that iOS will not allow requests of MTU size, and will always try to negotiate the highest possible MTU (iOS supports up to MTU size 185)

Reference

FlutterBlue API

AndroidiOSDescription
scanwhite_check_markwhite_check_markStarts a scan for Bluetooth Low Energy devices.
statewhite_check_markwhite_check_markStream of state changes for the Bluetooth Adapter.
isAvailablewhite_check_markwhite_check_markChecks whether the device supports Bluetooth.
isOnwhite_check_markwhite_check_markChecks if Bluetooth functionality is turned on.

BluetoothDevice API

AndroidiOSDescription
connectwhite_check_markwhite_check_markEstablishes a connection to the device.
disconnectwhite_check_markwhite_check_markCancels an active or pending connection to the device.
discoverServiceswhite_check_markwhite_check_markDiscovers services offered by the remote device as well as their characteristics and descriptors.
serviceswhite_check_markwhite_check_markGets a list of services. Requires that discoverServices() has completed.
statewhite_check_markwhite_check_markStream of state changes for the Bluetooth Device.
mtuwhite_check_markwhite_check_markStream of mtu size changes.
requestMtuwhite_check_markRequest to change the MTU for the device.

BluetoothCharacteristic API

AndroidiOSDescription
readwhite_check_markwhite_check_markRetrieves the value of the characteristic.
writewhite_check_markwhite_check_markWrites the value of the characteristic.
setNotifyValuewhite_check_markwhite_check_markSets notifications or indications on the characteristic.
valuewhite_check_markwhite_check_markStream of characteristic’s value when changed.

BluetoothDescriptor API

AndroidiOSDescription
readwhite_check_markwhite_check_markRetrieves the value of the descriptor.
writewhite_check_markwhite_check_markWrites the value of the descriptor.
阅读全文
资源下载
下载价格免费
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.shuli.cc/?p=7324,转载请注明出处。
0
显示验证码
没有账号?注册  忘记密码?