-
Notifications
You must be signed in to change notification settings - Fork 26
/
btScan.mm
69 lines (61 loc) · 2.44 KB
/
btScan.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Scanning for Bluetooth devices requires the RunLoop of the main thread to be executed.
// This cannot be guaranteed in a library which may be used from a Unix-style console
// application. Hence, BT scanning is implemented as this separate executable where
// we control the RunLoop.
//
#include <iostream>
#import <Foundation/NSObject.h>
#import <IOBluetooth/objc/IOBluetoothDevice.h>
#import <IOBluetooth/objc/IOBluetoothDeviceInquiry.h>
#include "json.hpp"
// The purpose of this delegate is to finish the RunLoop once the inquiry completes.
@interface DiscoveryFinisher : NSObject<IOBluetoothDeviceInquiryDelegate>
- (void) deviceInquiryComplete: (IOBluetoothDeviceInquiry *)sender error: (IOReturn)error aborted: (BOOL)aborted;
@end
@implementation DiscoveryFinisher
- (void)deviceInquiryComplete: (IOBluetoothDeviceInquiry *)sender error: (IOReturn)error aborted: (BOOL)aborted
{
CFRunLoopStop(CFRunLoopGetCurrent());
}
@end
nlohmann::json jsonForDevice(IOBluetoothDevice *device)
{
nlohmann::json j = {
{"addressString", [[device addressString] UTF8String]},
{"classOfDevice", [device classOfDevice]},
{"deviceClassMajor", [device deviceClassMajor]},
{"deviceClassMinor", [device deviceClassMinor]},
{"nameOrAddress", [[device nameOrAddress] UTF8String]},
{"rawRSSI", [device rawRSSI]},
{"isConnected", (bool)[device isConnected]},
{"isPaired", (bool)[device isPaired]},
{"isFavorite", (bool)[device isFavorite]},
{"lastSeen", [[device getLastInquiryUpdate] timeIntervalSince1970]},
{"haveName", [device name] != nil}
};
return j;
}
int main(int argc, const char* argv[])
{
@autoreleasepool {
DiscoveryFinisher* df = [[DiscoveryFinisher alloc] init];
IOBluetoothDeviceInquiry* bdi = [[IOBluetoothDeviceInquiry alloc] initWithDelegate: df];
[bdi setUpdateNewDeviceNames:NO];
if ([bdi start] != kIOReturnSuccess) {
// Output empty JS array.
printf("[]");
return -1;
}
CFRunLoopRun();
[bdi stop];
NSArray *foundDevices = [bdi foundDevices];
nlohmann::json j = nlohmann::json::array();
NSEnumerator *enumerator = [foundDevices objectEnumerator];
while (IOBluetoothDevice *dev = (IOBluetoothDevice *)[enumerator nextObject]) {
j.push_back(jsonForDevice(dev));
}
printf("%s", j.dump(2).c_str());
}
return 0;
}