Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions ios/Classes/FlutterBluetoothBasicPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ @interface FlutterBluetoothBasicPlugin ()
@property(nonatomic, retain) FlutterMethodChannel *channel;
@property(nonatomic, retain) BluetoothPrintStreamHandler *stateStreamHandler;
@property(nonatomic) NSMutableDictionary *scannedPeripherals;
@property(nonatomic,assign) NSInteger stateIndex;
@property(nonatomic,assign) NSInteger bluetoothState;
;
@end

@implementation FlutterBluetoothBasicPlugin
Expand All @@ -23,7 +26,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
BluetoothPrintStreamHandler* stateStreamHandler = [[BluetoothPrintStreamHandler alloc] init];
[stateChannel setStreamHandler:stateStreamHandler];
instance.stateStreamHandler = stateStreamHandler;

instance.stateIndex = -1;
instance.bluetoothState = 1;
[registrar addMethodCallDelegate:instance channel:channel];
}

Expand All @@ -33,35 +37,42 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"state" isEqualToString:call.method]) {
result(nil);
} else if([@"isAvailable" isEqualToString:call.method]) {

result(@(YES));
bool isAvailable = self.bluetoothState ==1;
result(@(isAvailable));
} else if([@"isConnected" isEqualToString:call.method]) {

result(@(NO));
}
else if([@"connectStateInteger" isEqualToString:call.method]) {
result(@(self.stateIndex));
} else if([@"isOn" isEqualToString:call.method]) {
result(@(YES));
}else if([@"startScan" isEqualToString:call.method]) {
NSLog(@"getDevices method -> %@", call.method);
[self.scannedPeripherals removeAllObjects];

if (Manager.bleConnecter == nil) {
[Manager didUpdateState:^(NSInteger state) {
switch (state) {
case CBCentralManagerStateUnsupported:
self.bluetoothState = 4;
NSLog(@"The platform/hardware doesn't support Bluetooth Low Energy.");
break;
case CBCentralManagerStateUnauthorized:
self.bluetoothState = 3;
NSLog(@"The app is not authorized to use Bluetooth Low Energy.");
break;
case CBCentralManagerStatePoweredOff:
self.bluetoothState = 2;
NSLog(@"Bluetooth is currently powered off.");
break;
case CBCentralManagerStatePoweredOn:
[self startScan];
self.bluetoothState = 1;
NSLog(@"Bluetooth power on");
break;
case CBCentralManagerStateUnknown:
default:

default: self.bluetoothState = -1;
break;
}
}];
Expand Down Expand Up @@ -139,23 +150,28 @@ -(void)updateConnectState:(ConnectState)state {
switch (state) {
case CONNECT_STATE_CONNECTING:
NSLog(@"status -> %@", @"Connecting ...");
self.stateIndex = 0;
ret = @0;
break;
case CONNECT_STATE_CONNECTED:
NSLog(@"status -> %@", @"Connection success");
ret = @1;
self.stateIndex = 1;
break;
case CONNECT_STATE_FAILT:
NSLog(@"status -> %@", @"Connection failed");
ret = @0;
self.stateIndex = 2;
break;
case CONNECT_STATE_DISCONNECT:
NSLog(@"status -> %@", @"Disconnected");
self.stateIndex = 3;
ret = @0;
break;
default:
NSLog(@"status -> %@", @"Connection timed out");
ret = @0;
self.stateIndex = -1;
break;
}

Expand Down
7 changes: 5 additions & 2 deletions lib/src/bluetooth_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ class BluetoothManager {

static BluetoothManager get instance => _instance;

// Future<bool> get isAvailable async =>
// await _channel.invokeMethod('isAvailable').then<bool>((d) => d);
Future<bool> get isAvailable async =>
await _channel.invokeMethod('isAvailable');

// Future<bool> get isOn async =>
// await _channel.invokeMethod('isOn').then<bool>((d) => d);

Future<bool> get isConnected async =>
await _channel.invokeMethod('isConnected');

Future<int> get connectStateInteger async =>
await _channel.invokeMethod('connectStateInteger');

BehaviorSubject<bool> _isScanning = BehaviorSubject.seeded(false);
Stream<bool> get isScanning => _isScanning.stream;

Expand Down