20140802 Bluetooth を使って Universall App で大連携

33

description

20140802 めとべや東京#5 での発表資料

Transcript of 20140802 Bluetooth を使って Universall App で大連携

Page 1: 20140802 Bluetooth を使って Universall App で大連携
Page 2: 20140802 Bluetooth を使って Universall App で大連携

仕事

個人活動

http://tanaka733.net

https://www.nuget.org/profiles/tanaka_733/

https://github.com/tanaka-takayoshi

Page 3: 20140802 Bluetooth を使って Universall App で大連携

3か月に1回程度の勉強会中心のコミュニティ

めと #とは

Page 4: 20140802 Bluetooth を使って Universall App で大連携
Page 5: 20140802 Bluetooth を使って Universall App で大連携

iPhoneやiPadで使えるドン!可愛さと実用性を兼ね備えた『太鼓の達人』専用Bluetoothコントローラ「太鼓とバチ」12月発売

Page 6: 20140802 Bluetooth を使って Universall App で大連携

なんでBluetooth?

Page 7: 20140802 Bluetooth を使って Universall App で大連携
Page 8: 20140802 Bluetooth を使って Universall App で大連携

マスター・スレイブ通信

リンク層プロトコル

Page 9: 20140802 Bluetooth を使って Universall App で大連携

ペアリング

プロファイル

サービスディスカバリ

Page 10: 20140802 Bluetooth を使って Universall App で大連携
Page 11: 20140802 Bluetooth を使って Universall App で大連携
Page 12: 20140802 Bluetooth を使って Universall App で大連携

BR/EDR (RFCOMM)

Bluetooth LTE (GATT)

Page 13: 20140802 Bluetooth を使って Universall App で大連携
Page 14: 20140802 Bluetooth を使って Universall App で大連携

詳細はブログに書いています

Texas InstrumentBluetooth Low Energy(CC2541)SensorTag

温度センサ湿度センサ圧力センサ加速度計ジャイロスコープ磁力計

Page 15: 20140802 Bluetooth を使って Universall App で大連携
Page 16: 20140802 Bluetooth を使って Universall App で大連携

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の開発

Page 17: 20140802 Bluetooth を使って Universall App で大連携

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

Page 18: 20140802 Bluetooth を使って Universall App で大連携
Page 19: 20140802 Bluetooth を使って Universall App で大連携

http://msdn.microsoft.com/ja-jp/library/windows/apps/dn263090.aspx

Page 20: 20140802 Bluetooth を使って Universall App で大連携

<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>

Page 21: 20140802 Bluetooth を使って Universall App で大連携

<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>

Page 22: 20140802 Bluetooth を使って Universall App で大連携

https://github.com/tanaka-takayoshi/BluetoothRfcommUniversalApp

Page 23: 20140802 Bluetooth を使って Universall App で大連携

//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);

Page 24: 20140802 Bluetooth を使って Universall App で大連携

//ペアリングされている端末を検出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();

Page 25: 20140802 Bluetooth を使って Universall App で大連携

writer = new DataWriter(socket.OutputStream);

//送信するバイトの長さ+byte配列を書きこみwriter.WriteUInt32((uint)Command.CommandLength);writer.WriteBytes(Command.CommandBytes);await writer.StoreAsync();

Page 26: 20140802 Bluetooth を使って Universall App で大連携

Bluetooth SMART デバイス(LE GATT Profile)と連携するWindows Store Appsの開発

https://github.com/tanaka-takayoshi/TISensor.Rx

Page 27: 20140802 Bluetooth を使って Universall App で大連携

//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());

Page 28: 20140802 Bluetooth を使って Universall App で大連携
Page 29: 20140802 Bluetooth を使って Universall App で大連携
Page 30: 20140802 Bluetooth を使って Universall App で大連携

dataCharacteristic = deviceService.GetCharacteristics(new Guid(sensorDataUuid)).First();

await dataCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(

GattClientCharacteristicConfigurationDescriptorValue.Notify);

dataCharacteristic.ValueChanged += (sender, args) =>{

ReadRawData(args);};

Page 31: 20140802 Bluetooth を使って Universall App で大連携

public IObservable<SensorRawValue> StartMonitor(){

returnObservable.FromEventPattern<

TypedEventHandler<GattCharacteristic,GattValueChangedEventArgs>,

GattValueChangedEventArgs>(h => dataCharacteristic.ValueChanged += h,h => dataCharacteristic.ValueChanged -= h)

.Select(p => ReadRawData(p.EventArgs));}

Page 32: 20140802 Bluetooth を使って Universall App で大連携

using

Page 33: 20140802 Bluetooth を使って Universall App で大連携