flutter_blue_elves
a flutter plugin witch includes platform-specific implementation code for android and/or ios to connect and control bluetooth ble device.
install
this project is a flutter plugin,so you use it by add dependencies in your pubspec.yaml.
dependencies:
flutter:
sdk: flutter
flutter_blue_elves: ^0.1.0
$ flutter pub get
usage
import plugin module where you need use.
import 'package:flutter_blue_elves/flutter_blue_elves.dart';
check bluetooth function is ok.
///android:
flutterblueelves.instance.androidcheckbluelackwhat().then((values) {
if(values.contains(androidbluetoothlack.locationpermission)){
///no location permission
}
if(values.contains(androidbluetoothlack.locationfunction)){
///location poweroff
}
if(values.contains(androidbluetoothlack.bluetoothfunction)){
///bluetooth poweroff
}
});
///ios:
flutterblueelves.instance.ioscheckbluetoothstate().then((value) {
if(value==iosbluetoothstate.unknown){
///bluetooth is not initialized
}else if(value==iosbluetoothstate.resetting){
///bluetooth is resetting
}else if(value==iosbluetoothstate.unsupport){
///bluetooth not support
}else if(value==iosbluetoothstate.unauthorized){
///no give bluetooth permission
}else if(value==iosbluetoothstate.poweredoff){
///bluetooth poweroff
}else{
///bluetooth is ok
}
}
turn on bluetooth function what bluetooth need.just for android.
///apply location permission
flutterblueelves.instance.androidapplylocationpermission((isok) {
print(isok ? "user agrees to grant location permission" : "user does not agree to grant location permission");
});
///turn on location function
flutterblueelves.instance.androidopenlocationservice((isok) {
print(isok ? "the user agrees to turn on the positioning function" : "the user does not agree to enable the positioning function");
});
///turn on bluetooth function
flutterblueelves.instance.androidopenbluetoothservice((isok) {
print(isok ? "the user agrees to turn on the bluetooth function" : "the user does not agrees to turn on the bluetooth function");
});
scan bluetooth device not connected.
///start scan,you can set scan timeout
flutterblueelves.instance.startscan(5000).listen((scanitem) {
///use the information in the scanned object to filter the devices you want
///if want to connect someone,call scanitem.connect,it will return device object
device device = scanitem.connect(connecttimeout: 5000);
///you can use this device to listen bluetooth device's state
device.statestream.listen((newstate){
///newstate is devicestate type,include disconnected,disconnecting, connecting,connected, connecttimeout,initiativedisconnected,destroyed
}).ondone(() {
///if scan timeout or you stop scan,will into this
});
});
///stop scan
flutterblueelves.instance.stopscan();
discovery device’s bluetooth service.
///use this stream to listen discovery result
device.servicediscoverystream.listen((serviceitem) {
///serviceitem type is bleservice,is readonly.it include blecharacteristic and bledescriptor
});
///to discovery service,witch work in connected
device.discoveryservice();
communicate with the device,witch work in connected.
///use this stream to listen data result
device.devicesignalresultstream.listen((result) {
///result type is devicesignalresult,is readonly.it have devicesignaltype attributes,witch include characteristicsread,characteristicswrite,characteristicsnotify,descriptorread,descriptorwrite,unknown.
///in ios you will accept unknown,because characteristicsread is as same as characteristicsnotify for ios.so characteristicsread or characteristicsnotify will return unknown.
});
///read data from device by characteristic.
///to read,witch work in connected
device.readdata(serviceuuid,characteristicuuid);
///write data to device by characteristic.
///to write,witch work in connected.after my test,i find this isnoresponse is work in ios but not in android.
///in ios if you set isnoresponse,you will not receive data after write,but android will.
device.writedata(serviceuuid,characteristicuuid,isnoresponse,data);
///read data from device by descriptor.
device.devicereaddescriptordata(serviceuuid,characteristicuuid);
///write data to device by descriptor.
device.writedescriptordata(serviceuuid,characteristicuuid,data);
connect&disconnect device
///work in disconnected
///this connection method is direct connection, because it has been connected before, so i saved the direct connection object, and i can use this object to connect again
device.connect(5000);
///work in connected
///disconnect device
device.disconnect();
destroy device object
///if you never use this device object,call destroy(),witch can save space
device.destroy();
license
mit © pineappleoilprince
Comments are closed.