Sie sind auf Seite 1von 2

32feet.net howto discover nearby bluetooth devices async in c# bool PairDevice() { using (var discoverForm = new SelectBluetoothDeviceDialog()) { if (discoverForm.

ShowDialog(this) != DialogResult.OK) { // no device selected return false; } BluetoothDeviceInfo deviceInfo = discoverForm.SelectedDevice; if (!deviceInfo.Authenticated) // previously paired? { // TODO: show a dialog with a PIN/discover the device PIN if (!BluetoothSecurity.PairDevice(deviceInfo.DeviceAddress, myPin)) { // not previously paired and attempt to pair failed return false; } } // device should now be paired with the OS so make a connection to it as ynchronously var client = new BluetoothClient(); client.BeginConnect(deviceInfo.DeviceAddress, BluetoothService.SerialPor t, this.BluetoothClientConnectCallback, client); return true; } } void BluetoothClientConnectCallback(IAsyncResult result) { var client = (BluetoothClient)result.State; client.EndConnect(); // get the client's stream and do whatever reading/writing you want to do. // if you want to maintain the connection then calls to Read() on the client 's stream should block when awaiting data from the device // when you're done reading/writing and want to close the connection or the device servers the connection control flow will resume here and you need to tidy up client.Close(); } void SetupListener() { var listener = new BluetoothListener(BluetoothService.SerialPort); listener.Start(); listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallba ck, listener); } void BluetoothListenerAcceptClientCallback(IAsyncResult result) { var listener = (BluetoothListener)result.State;

// continue listening for other broadcasting devices listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallba ck, listener); // create a connection to the device that's just been found BluetoothClient client = listener.EndAcceptBluetoothClient(); // the method we're in is already asynchronous and it's already connected to the client (via EndAcceptBluetoothClient) so there's no need to call BeginConne ct // TODO: perform your reading/writing as you did in the first code sample client.Close(); } void ScanForBluetoothClients() { var client = new BluetoothClient(); BluetoothDeviceInfo[] availableDevices = client.DiscoverDevices(); // I've f ound this to be SLOW! foreach (BluetoothDeviceInfo device in availableDevices) { if (!device.Authenticated) { continue; } var peerClient = new BluetoothClient(); peerClient.BeginConnect(deviceInfo.DeviceAddress, BluetoothService.Seria lPort, this.BluetoothClientConnectCallback, peerClient); } } peerClient.BeginConnect(deviceInfo.DeviceAddress, BluetoothService.SerialPort, t his.BluetoothClientConnectCallback, peerClient); void BluetoothClientConnectCallback(IAsyncResult result) { ............. } System.Net.Sockets.SocketException was unhandled by user code HResult=-2147467259 Message=The requested address is not valid in its context 0014A48BBD21:000011010 0001000800000805f9b34fb Source=System ErrorCode=10049 NativeErrorCode=10049 StackTrace: at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre ss socketAddress) at System.Net.Sockets.Socket.Connect(EndPoint remoteEP) at InTheHand.Net.Bluetooth.Msft.SocketBluetoothClient.Connect(BluetoothEndPoi nt remoteEP) at InTheHand.Net.Sockets.BluetoothClient.Connect(BluetoothEndPoint remoteEP) at InTheHand.Net.Sockets.BluetoothClient.Connect(BluetoothAddress address, Gu id service) at MyNamespace.DesktopApp. ................................

Das könnte Ihnen auch gefallen