20140802 bluetooth を使って universall app で大連携
DESCRIPTION
20140802 めとべや東京#5 での発表資料TRANSCRIPT
仕事
個人活動
http://tanaka733.net
https://www.nuget.org/profiles/tanaka_733/
https://github.com/tanaka-takayoshi
3か月に1回程度の勉強会中心のコミュニティ
めと #とは
iPhoneやiPadで使えるドン!可愛さと実用性を兼ね備えた『太鼓の達人』専用Bluetoothコントローラ「太鼓とバチ」12月発売
なんでBluetooth?
マスター・スレイブ通信
リンク層プロトコル
ペアリング
プロファイル
サービスディスカバリ
BR/EDR (RFCOMM)
Bluetooth LTE (GATT)
詳細はブログに書いています
Texas InstrumentBluetooth Low Energy(CC2541)SensorTag
温度センサ湿度センサ圧力センサ加速度計ジャイロスコープ磁力計
RFCOMM Scenario: Send File as a Client(XAML)
RFCOMM Scenario: Receive File as a Server(XAML)
GATT Scenario: Retrieve Bluetooth LE Data(XAML)
GATT Scenario: Control a Bluetooth LE Thermometer Device(XAML)
Bluetooth for Windows Phone 8
【連載】Bluetooth LE (1) Bluetooth Low Energy の基礎
Bluetooth SMART デバイス(LE GATT Profile)と連携するWindows Store Appsの開発
http://code.msdn.microsoft.com/windowsapps/Bluetooth-Rfcomm-Chat-afcee559/view/Discussions#content
http://code.msdn.microsoft.com/windowsapps/Bluetooth-Generic-5a99ef95/
http://blogs.msdn.com/b/flecoqui/archive/2013/12/13/windows-store-and-windows-phone-app-to-app-communication-over-bluetooth.aspx
http://msdn.microsoft.com/ja-jp/library/windows/apps/dn263090.aspx
<Capabilities><m2:DeviceCapability Name="bluetooth.rfcomm"><m2:Device Id="any">
<m2:Function Type="serviceId:BEFB3B5F-C81D-48BB-8136-72E6BC3D0169"/></m2:Device>
</m2:DeviceCapability><m2:DeviceCapability Name="bluetooth.genericAttributeProfile"><m2:Device Id="any">
<m2:Function Type="serviceId:f000aa00-0451-4000-b000-000000000000" /><m2:Function Type="serviceId:f000aa10-0451-4000-b000-000000000000" />
</m2:Device></m2:DeviceCapability>
</Capabilities>
<Capabilities><m2:DeviceCapability Name="bluetooth.rfcomm"><m2:Device Id="any">
<m2:Function Type="serviceId:BEFB3B5F-C81D-48BB-8136-72E6BC3D0169"/></m2:Device>
</m2:DeviceCapability><m2:DeviceCapability Name="bluetooth.genericAttributeProfile"><m2:Device Id="any">
<m2:Function Type="serviceId:f000aa00-0451-4000-b000-000000000000" /><m2:Function Type="serviceId:f000aa10-0451-4000-b000-000000000000" />
</m2:Device></m2:DeviceCapability>
</Capabilities>
https://github.com/tanaka-takayoshi/BluetoothRfcommUniversalApp
//appxmanifet で指定したServiceUUIDを使うrfcommProvider = await RfcommServiceProvider.CreateAsync(
RfcommServiceId.FromUuid(BluetoothServiceUuid));
//Socket作成して、Clientからの接続を待機socketListener = new StreamSocketListener();socketListener.ConnectionReceived += OnConnectionReceived;
await socketListener.BindServiceNameAsync(rfcommProvider.ServiceId.AsString(),SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
//SDPに設定する部分は略//Advertising開始rfcommProvider.StartAdvertising(socketListener);
//ペアリングされている端末を検出PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";var Peers = await PeerFinder.FindAllPeersAsync();//検出した端末から1台選択var device =
(await BluetoothRfcommClient.GetListPairedDevices()).First();
socket = new StreamSocket();await socket.ConnectAsync(
new HostName(device.HostName), BluetoothServiceName);
//Advertising停止PeerFinder.Stop();
writer = new DataWriter(socket.OutputStream);
//送信するバイトの長さ+byte配列を書きこみwriter.WriteUInt32((uint)Command.CommandLength);writer.WriteBytes(Command.CommandBytes);await writer.StoreAsync();
Bluetooth SMART デバイス(LE GATT Profile)と連携するWindows Store Appsの開発
https://github.com/tanaka-takayoshi/TISensor.Rx
//SensorごとのUUIDを指定して端末を検出するvar selector = GattDeviceService.GetDeviceSelectorFromUuid(
new Guid(sensorServiceUuid));var devices = await DeviceInformation.FindAllAsync(selector);var device = devices.First();
//ID指定でサービスを作成deviceService = await GattDeviceService.FromIdAsync(deviceInfo.Id);
//センサーを有効化 (センサーによっては書きこむ値が違う)var configCharacteristic = deviceService.GetCharacteristics(
new Guid(sensorConfigUuid)).First();await configCharacteristic.WriteValueAsync(new byte[] {1}.AsBuffer());
dataCharacteristic = deviceService.GetCharacteristics(new Guid(sensorDataUuid)).First();
await dataCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
GattClientCharacteristicConfigurationDescriptorValue.Notify);
dataCharacteristic.ValueChanged += (sender, args) =>{
ReadRawData(args);};
public IObservable<SensorRawValue> StartMonitor(){
returnObservable.FromEventPattern<
TypedEventHandler<GattCharacteristic,GattValueChangedEventArgs>,
GattValueChangedEventArgs>(h => dataCharacteristic.ValueChanged += h,h => dataCharacteristic.ValueChanged -= h)
.Select(p => ReadRawData(p.EventArgs));}
using