The Bluetooth simple key service example demonstrates how to access Bluetooth Low-energy Ti simple key services from Metro applications by using Windows portable devices (WPD) drivers and WPD automated APIs.
(Function(){
VaR Simplekeyservice = Null ;
function outputstatus (Message) {
var statuselement = document. getelementbyid ("statusmessage");
If (statuselement) {
statuselement. innertext = message;
}< BR >}
function applicationactivated () {
try {< br> If (tempservice! = null ) {
tempservice. applicationactivated ();
}< BR >}< span style = "color: # 0000ff;"> catch (exception) {
sdksample. displayerror (exception. tostring ();
}< BR >}
function applicationsuspended () {
try {< br> If (tempservice! = null ) {
tempservice. applicationsuincluded ();
}< BR >}< span style = "color: # 0000ff;"> catch (exception) {
sdksample. displayerror (exception. tostring ();
}< BR >}
Winjs. namespace. Define ('tikeyfobsample. descrithcontroller ',{
Applicationactivated: applicationactivated,
Applicationsuspended: applicationsuspended,
Initializecontrollerdevices: Function (){
Outputstatus ("looking for Bluetooth controllers ...");
// Use WPD automation to query for devices that implement the simple key service
Windows. devices. enumeration. deviceinformation. findallasync ("system. devices. interfaceclassguid: = \" "+ tikeyfobsample. constants. simplekeydeviceuuid + "\"",
Null ).
Then ( Function (Devices ){
If (Devices. length> 0 ){
Outputstatus ("found a Bluetooth controller ...");
Try {
VaR Devicefactory = New Activexobject ("portabledeviceautomation. Factory ");
VaR Device = devicefactory. getdevicefromid (devices [0]. ID );
// Make sure that gdevservice is instantiated as a global to ensure it doesn' t get garbage collected
Simplekeyservice = device. Services [0];
// Register for custom onkeypressed event
Simplekeyservice. onkeypressed = Function (Keypressvalue ){
VaR Leftbutton = Document. getelementbyid ("leftbutton ");
VaR Rightbutton = Document. getelementbyid ("rightbutton ");
Outputstatus ("received a value of:" + keypressvalue. tostring ());
Switch (Keypressvalue ){
// No button is pressed
Case 0:
Leftbutton. style. Background = "red ";
Rightbutton. style. Background = "red ";
Break ;
// Left button is pressed
Case 1:
Leftbutton. style. Background = "green ";
Rightbutton. style. Background = "red ";
Break ;
// Right button is pressed
Case 2:
Leftbutton. style. Background = "red ";
Rightbutton. style. Background = "green ";
Break ;
// Both buttons are pressed
Case 3:
Leftbutton. style. Background = "green ";
Rightbutton. style. Background = "green ";
Break ;
Default :
Break ;
};
};
Simplekeyservice. applicationactivated ();
}Catch (E ){
Outputstatus ("error locating a Bluetooth Controller:" + E. Message );
}
}Else{
Outputstatus ("no Bluetooth controller found ");
}
});
}
});
})();
Complete example/files/risk/windows8/simple master service sample.rar