Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: device/bluetooth/bluetooth_low_energy_device_mac.mm

Issue 791763005: Added bluetooth LE support on Mac platform (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: feedback. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
6
7 #import <CoreFoundation/CoreFoundation.h>
8
9 #include "base/mac/scoped_cftyperef.h"
10
11 using namespace device;
Avi (use Gerrit) 2015/01/29 05:25:02 This doesn't work, and if you tried to compile you
dvh 2015/01/30 22:45:45 I'm not sure to understand what you mean here. Cou
Avi (use Gerrit) 2015/01/30 23:18:31 Do not use "using" in this way. First, it's forbi
12
13 namespace {
14
15 // Converts a CBUUID to a Cocoa string.
16 //
17 // The string representation can have the following formats:
18 // - 16 bit: xxxx
19 // - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
20 // CBUUID supports only 16 bits and 128 bits formats.
21 //
22 // In OSX < 10.10, -[uuid UUIDString] method is not implemented. It's why we
23 // need to provide this function.
24 NSString* stringWithCBUUID(CBUUID* uuid) {
25 NSData* data = [uuid data];
26
27 NSUInteger bytesToConvert = [data length];
28 const unsigned char* uuidBytes = (const unsigned char*)[data bytes];
29 NSMutableString* outputString = [NSMutableString stringWithCapacity:16];
30
31 for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert;
32 currentByteIndex++) {
33 switch (currentByteIndex) {
34 case 3:
35 case 5:
36 case 7:
37 case 9:
38 [outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]];
39 break;
40 default:
41 [outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]];
42 }
43 }
44 return outputString;
45 }
46
47 // Converts a CBUUID to a BluetoothUUID.
48 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) {
49 NSString* uuidString = nil;
50 if ([uuid respondsToSelector:@selector(UUIDString)]) {
51 uuidString = [uuid performSelector:@selector(UUIDString)];
52 } else {
53 uuidString = stringWithCBUUID(uuid);
54 }
55 std::string uuid_c_string([uuidString UTF8String]);
56 return BluetoothUUID(uuid_c_string);
57 }
58
59 } // namespace device
Avi (use Gerrit) 2015/01/29 05:25:02 this does not end a namespace "device"; it ends an
dvh 2015/01/30 22:45:45 Done.
60
61 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac(
62 CBPeripheral* peripheral,
63 NSDictionary* advertisementData,
64 int rssi) {
65 Update(peripheral, advertisementData, rssi);
66 }
67
68 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() {
69 }
70
71 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral,
72 NSDictionary* advertisementData,
73 int rssi) {
74 peripheral_.reset([peripheral retain]);
75 rssi_ = rssi;
76 ClearServiceData();
77 NSNumber* nbConnectable =
78 [advertisementData objectForKey:CBAdvertisementDataIsConnectable];
79 connectable_ = [nbConnectable boolValue];
80 NSDictionary* serviceData =
81 [advertisementData objectForKey:CBAdvertisementDataServiceDataKey];
82 for (CBUUID* uuid in serviceData) {
83 NSData* data = [serviceData objectForKey:uuid];
84 BluetoothUUID serviceUUID = BluetoothUUIDWithCBUUID(uuid);
85 SetServiceData(serviceUUID, (const char*)[data bytes], [data length]);
86 }
87 }
88
89 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const {
90 if ([peripheral_ respondsToSelector:@selector(identifier)]) {
91 // When -[CBPeripheral identifier] is available.
92 NSUUID* uuid = [peripheral_ performSelector:@selector(identifier)];
93 NSString* uuidString = [uuid UUIDString];
94 return std::string([uuidString UTF8String]);
Avi (use Gerrit) 2015/01/29 05:25:02 SysNSStringToUTF8
dvh 2015/01/30 22:45:45 Done.
95 }
96
97 base::ScopedCFTypeRef<CFStringRef> str(
98 CFUUIDCreateString(NULL, [peripheral_ UUID]));
99 base::ScopedCFTypeRef<CFDataRef> data(CFStringCreateExternalRepresentation(
100 NULL, str, kCFStringEncodingASCII, 0));
101 return std::string((const char*)CFDataGetBytePtr(data),
102 CFDataGetLength(data));
Avi (use Gerrit) 2015/01/29 05:25:02 Huh? SysCFStringRefToUTF8.
dvh 2015/01/30 22:45:45 Done.
103 }
104
105 uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const {
106 return 0;
107 }
108
109 std::string BluetoothLowEnergyDeviceMac::GetAddress() const {
110 return std::string();
111 }
112
113 BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource()
114 const {
115 return VENDOR_ID_UNKNOWN;
116 }
117
118 uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const {
119 return 0;
120 }
121
122 uint16 BluetoothLowEnergyDeviceMac::GetProductID() const {
123 return 0;
124 }
125
126 uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const {
127 return 0;
128 }
129
130 int BluetoothLowEnergyDeviceMac::GetRSSI() const {
131 return rssi_;
132 }
133
134 bool BluetoothLowEnergyDeviceMac::IsPaired() const {
135 return false;
136 }
137
138 bool BluetoothLowEnergyDeviceMac::IsConnected() const {
139 return [peripheral_ isConnected];
140 }
141
142 bool BluetoothLowEnergyDeviceMac::IsConnectable() const {
143 return connectable_;
144 }
145
146 bool BluetoothLowEnergyDeviceMac::IsConnecting() const {
147 return false;
148 }
149
150 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const {
151 return std::vector<device::BluetoothUUID>();
152 }
153
154 bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const {
155 return false;
156 }
157
158 bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const {
159 return false;
160 }
161
162 bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const {
163 return false;
164 }
165
166 void BluetoothLowEnergyDeviceMac::GetConnectionInfo(
167 const ConnectionInfoCallback& callback) {
168 NOTIMPLEMENTED();
169 }
170
171 void BluetoothLowEnergyDeviceMac::Connect(
172 PairingDelegate* pairing_delegate,
173 const base::Closure& callback,
174 const ConnectErrorCallback& error_callback) {
175 NOTIMPLEMENTED();
176 }
177
178 void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) {
179 NOTIMPLEMENTED();
180 }
181
182 void BluetoothLowEnergyDeviceMac::SetPasskey(uint32 passkey) {
183 NOTIMPLEMENTED();
184 }
185
186 void BluetoothLowEnergyDeviceMac::ConfirmPairing() {
187 NOTIMPLEMENTED();
188 }
189
190 void BluetoothLowEnergyDeviceMac::RejectPairing() {
191 NOTIMPLEMENTED();
192 }
193
194 void BluetoothLowEnergyDeviceMac::CancelPairing() {
195 NOTIMPLEMENTED();
196 }
197
198 void BluetoothLowEnergyDeviceMac::Disconnect(
199 const base::Closure& callback,
200 const ErrorCallback& error_callback) {
201 NOTIMPLEMENTED();
202 }
203
204 void BluetoothLowEnergyDeviceMac::Forget(const ErrorCallback& error_callback) {
205 NOTIMPLEMENTED();
206 }
207
208 void BluetoothLowEnergyDeviceMac::ConnectToService(
209 const BluetoothUUID& uuid,
210 const ConnectToServiceCallback& callback,
211 const ConnectToServiceErrorCallback& error_callback) {
212 NOTIMPLEMENTED();
213 }
214
215 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely(
216 const device::BluetoothUUID& uuid,
217 const ConnectToServiceCallback& callback,
218 const ConnectToServiceErrorCallback& error_callback) {
219 NOTIMPLEMENTED();
220 }
221
222 void BluetoothLowEnergyDeviceMac::CreateGattConnection(
223 const GattConnectionCallback& callback,
224 const ConnectErrorCallback& error_callback) {
225 NOTIMPLEMENTED();
226 }
227
228 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const {
229 NSData* data = [[peripheral_ name] dataUsingEncoding:NSUTF8StringEncoding
230 allowLossyConversion:YES];
231 return std::string((const char*)[data bytes], [data length]);
Avi (use Gerrit) 2015/01/29 05:25:02 SysNSStringToUTF8
dvh 2015/01/30 22:45:45 Done.
232 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698