Skip to content

Commit

Permalink
Merge pull request #296 from backbonelabs/direct-device-address-fetch
Browse files Browse the repository at this point in the history
Store Device Identifier
  • Loading branch information
kevhuang authored May 1, 2017
2 parents 94170dc + 71c95ad commit c706116
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static BluetoothService getInstance(ReactApplicationContext reactContext)
};

private BluetoothDevice currentDevice = null;
private String currentDeviceIdentifier;
private String currentDeviceIdentifier = "";
private int currentDeviceMode = Constants.DEVICE_MODES.UNKNOWN;

private ReactApplicationContext reactContext;
Expand Down Expand Up @@ -492,6 +492,10 @@ public BluetoothDevice getCurrentDevice() {
return currentDevice;
}

public String getCurrentDeviceIdentifier() {
return currentDeviceIdentifier;
}

public void selectDevice(BluetoothDevice device) {
if (device != null) {
currentDevice = device;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,18 @@ public void onIntCallBack(int level) {
Timber.d("Found battery %d", level);
hasPendingCallback = false;

WritableMap wm = Arguments.createMap();
wm.putInt("deviceMode", bluetoothService.getCurrentDeviceMode());
wm.putString("identifier", bluetoothService.getCurrentDevice().getAddress());
wm.putString("firmwareVersion", version);
wm.putInt("batteryLevel", level);

callback.invoke(null, wm);
if (bluetoothService.isDeviceReady()) {
WritableMap wm = Arguments.createMap();
wm.putInt("deviceMode", bluetoothService.getCurrentDeviceMode());
wm.putString("identifier", bluetoothService.getCurrentDeviceIdentifier());
wm.putString("firmwareVersion", version);
wm.putInt("batteryLevel", level);

callback.invoke(null, wm);
}
else {
callback.invoke(JSError.make("Not connected to a device"));
}
}
});
}
Expand All @@ -95,7 +100,7 @@ public void onIntCallBack(int level) {

WritableMap wm = Arguments.createMap();
wm.putInt("deviceMode", bluetoothService.getCurrentDeviceMode());
wm.putString("identifier", bluetoothService.getCurrentDevice().getAddress());
wm.putString("identifier", bluetoothService.getCurrentDeviceIdentifier());
wm.putString("firmwareVersion", "");
wm.putInt("batteryLevel", -1);

Expand Down
1 change: 1 addition & 0 deletions ios/backbone/BluetoothService.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@property (nonatomic, copy) ErrorHandler connectHandler;
@property (nonatomic, copy) ErrorHandler disconnectHandler;
@property (nonatomic, strong) CBPeripheral *currentDevice;
@property (nonatomic, strong) NSString *currentDeviceIdentifier;

@property int state;
@property int currentDeviceMode;
Expand Down
3 changes: 3 additions & 0 deletions ios/backbone/BluetoothService.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ - (id)initService {

_state = CBCentralManagerStateUnknown;
_currentDeviceMode = DEVICE_MODE_UNKNOWN;
_currentDeviceIdentifier = @"";

stateMap = @{
@"0": [NSNumber numberWithInteger:-1],
Expand Down Expand Up @@ -217,6 +218,8 @@ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPerip
DLog(@"didconnect %@", peripheral);
_currentDevice = [peripheral copy];
_currentDevice.delegate = self;

_currentDeviceIdentifier = [peripheral.identifier.UUIDString copy];

[_currentDevice discoverServices:@[BACKBONE_SERVICE_UUID, BOOTLOADER_SERVICE_UUID, BATTERY_SERVICE_UUID]];
}
Expand Down
11 changes: 9 additions & 2 deletions ios/backbone/DeviceInformationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,21 @@ - (id)init {
[self retrieveFirmwareVersion:^(NSString * _Nonnull str) {
[self retrieveBatteryLevel:^(int value) {
hasPendingCallback = NO;
callback(@[[NSNull null], @{@"deviceMode" : @(BluetoothServiceInstance.currentDeviceMode), @"firmwareVersion" : str, @"batteryLevel" : @(value), @"identifier" : [BluetoothServiceInstance.currentDevice.identifier UUIDString] }]);

if ([BluetoothServiceInstance isDeviceReady]) {
callback(@[[NSNull null], @{@"deviceMode" : @(BluetoothServiceInstance.currentDeviceMode), @"firmwareVersion" : str, @"batteryLevel" : @(value), @"identifier" : BluetoothServiceInstance.currentDeviceIdentifier }]);
}
else {
NSDictionary *makeError = RCTMakeError(@"Not connected to a device", nil, nil);
callback(@[makeError]);
}
}];
}];
}
else {
// Required characteristics are not available, return default values
hasPendingCallback = NO;
callback(@[[NSNull null], @{@"deviceMode" : @(BluetoothServiceInstance.currentDeviceMode), @"firmwareVersion" : @"", @"batteryLevel" : @(-1), @"identifier" : [BluetoothServiceInstance.currentDevice.identifier UUIDString] }]);
callback(@[[NSNull null], @{@"deviceMode" : @(BluetoothServiceInstance.currentDeviceMode), @"firmwareVersion" : @"", @"batteryLevel" : @(-1), @"identifier" : BluetoothServiceInstance.currentDeviceIdentifier }]);
}
}
else {
Expand Down

0 comments on commit c706116

Please sign in to comment.