This article describes how to enable and disable Bluetooth. In subsequent articles, we will describe how to use Bluetooth for file transmission.
Because the WM simulator does not support Bluetooth debugging, If You Want To debug the code, debug it on a real device.
Enabling/disabling Bluetooth depends on one API (bthsetmode in bthutil. dll)
There are three Bluetooth modes:
Poweroff turn off Bluetooth/connectable turn on Bluetooth/discoverable turn on Bluetooth and can be found.
Differences between connectable and discoverable:
Connectable: Although Bluetooth is enabled, it cannot be found for Bluetooth devices enabled later than it.
Discoverable: enables Bluetooth and can be detected by other devices.
Main Code:
1 bluetoothmode mode;
2
3 Switch (this. combobox1.selectedindex)
4 {
5 case 0:
6 mode = effecthmode. poweroff;
7 break;
8 Case 1:
9 mode = systthmode. connectable;
10 break;
11 case 2:
12 mode = descrithmode. discoverable;
13 break;
14 default:
15 mode = effecthmode. poweroff;
16 break;
17}
18
19 int result = bthsetmode (mode );
20 if (result! = 0)
21 MessageBox. Show (string. Format ("error: {0}", Marshal. getlastwin32error (). tostring ()));
22 else
23 getdomainthstatue ();
API Definition
1 [dllimport ("bthutil. dll", setlasterror = true)]
2 public static extern int bthsetmode (effecthmode dwmode );
3
4 [dllimport ("bthutil. dll", setlasterror = true)]
5 public static extern int bthgetmode (out of memory thmode dwmode );
At this point, a simple bluetooth device controller can be implemented. The demo is relatively simple and provides inspiration to beginners.
Download the code: devicebluw.thdemo.rar
Author: appleseeker
Date: 2008-09-15