例子:Bluetooth app to device sample
2013-08-14 17:54
381 查看
本例子演示了:
判断蓝牙是否打开,是通过一个HRsult值为0x8007048F的异常来判断的
找到所有配对设备 - Windows.Networking.Proximity.PeerFinder
尝试连接已配对设备
//从数据中获取已经选中的蓝牙设备,在ConnectToDevice方法中连接
ConnectToDevice:
当捕获蓝牙未打开的异常,后打开蓝牙设置页面:(Microsoft.Phone.Tasks)
判断蓝牙是否打开,是通过一个HRsult值为0x8007048F的异常来判断的
catch (Exception ex) { if ((uint)ex.HResult == 0x8007048F) { var result = MessageBox.Show(AppResources.Msg_BluetoothOff, "Bluetooth Off", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { ShowBluetoothcControlPanel(); } }
找到所有配对设备 - Windows.Networking.Proximity.PeerFinder
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = ""; var peers = await PeerFinder.FindAllPeersAsync();
尝试连接已配对设备
//从数据中获取已经选中的蓝牙设备,在ConnectToDevice方法中连接
PairedDeviceInfo pdi = PairedDevicesList.SelectedItem as PairedDeviceInfo; PeerInformation peer = pdi.PeerInfo; // Asynchronous call to connect to the device ConnectToDevice(peer);
ConnectToDevice:
Windows.Networking.Sockets; try { Windows.Networking.Sockets.StreamSocket _socket = new StreamSocket(); string serviceName = (String.IsNullOrWhiteSpace(peer.ServiceName)) ? tbServiceName.Text : peer.ServiceName; // Note: If either parameter is null or empty, the call will throw an exception await _socket.ConnectAsync(peer.HostName, serviceName); // If the connection was successful, the RemoteAddress field will be populated MessageBox.Show(String.Format(AppResources.Msg_ConnectedTo, _socket.Information.RemoteAddress.DisplayName)); }
当捕获蓝牙未打开的异常,后打开蓝牙设置页面:(Microsoft.Phone.Tasks)
private void ShowBluetoothcControlPanel() { ConnectionSettingsTask connectionSettingsTask = new ConnectionSettingsTask(); connectionSettingsTask.ConnectionSettingsType = ConnectionSettingsType.Bluetooth; connectionSettingsTask.Show(); }
相关文章推荐
- How Do I Deploy a Windows 8 App to Another Device for Testing?
- “Incorrect Architecture” when trying to install iPhone app onto my development device
- *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DeviceStatusDetail
- iPhone SDK 3/4 App will not run on a iPhone 2.x device even with deployment target set to 2.0!
- Failed to install MyApp.apk on device '0123456789ABCDEF': timeout
- Directx Sample Browser中的DXUT例子Failed Creating Direct3d Device 的解决方案
- java.lang.ClassCastException: android.app.Application cannot be cast to serialport_api.sample.App问题
- java.lang.ClassCastException: android.app.Application cannot be cast to serialport_api.sample.App问题
- Migrating Folder Property sample app to Vault 2012
- 安卓ApiDemos学习 app/Device Admin/DeviceAdminSample
- java.lang.ClassCastException: android.app.Application cannot be cast to serialport_api.sample.App问题
- “Failed to upload *.app on Device ” Error in iPhone
- “Failed to upload *.app on Device ” Error in iPhone
- Create spi app to read data from SPI device use QNX 6.6
- How to Have Two Versions of the Same App on Your Device
- 安装APP出错,Android Unable to identify the apk for variant arm-debug and device
- java.lang.ClassCastException: android.app.Application cannot be cast to serialport_api.sample.App问题
- Failed to upload *.app on Device 可能的解决方法
- How to distribute iOS app to device or app store or testflight
- 连接你的APP至WiFi设备(Connecting your App to a Wi-Fi Device)