123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- part of flutter_bluetooth;
- class BluetoothDevice {
- final String name;
- final String address;
- final int deviceClass;
- BluetoothDevice({this.name, this.address, this.deviceClass});
- static BluetoothDevice fromJson(data) {
- String name = data['name'];
- String address = data['address'];
- int deviceClass = data['deviceClass'];
- return BluetoothDevice(name: name, address: address, deviceClass: deviceClass);
- }
- static Future<BluetoothDevice> fromAddress(String address) async {
- String name =
- await FlutterBluetooth._channel.invokeMethod('getName', address);
- int deviceClass =
- await FlutterBluetooth._channel.invokeMethod('getDeviceClass', address);
- return BluetoothDevice(
- address: address,
- name: name,
- deviceClass: deviceClass);
- }
- Future<bool> isBond() async {
- return FlutterBluetooth._channel.invokeMethod('isBond', address);
- }
- Future<bool> createBond() async {
- return FlutterBluetooth._channel.invokeMethod('createBond', address);
- }
- Future<BluetoothSocket> createSocket(String uuid) async {
- await FlutterBluetooth._channel.invokeMethod('createSocket', {
- 'address': address,
- 'uuid': uuid
- });
- return BluetoothSocket._(address, uuid);
- }
- Future<List<String>> getUUIDs() async{
- var isBond = await this.isBond();
- if(isBond){
- List uuids = await FlutterBluetooth._channel.invokeMethod('getUUIDs', address);
- return uuids.map((item)=>item as String).toList();
- }else{
- throw ErrorDescription("只有配对蓝牙后才可以获取UUID");
- }
- }
- bool operator ==(target){
- if(target is BluetoothDevice){
- return this.address == target.address;
- }else{
- return false;
- }
- }
- @override
- // TODO: implement hashCode
- int get hashCode => this.address.hashCode;
- }
|