mirror of
https://github.com/purplecabbage/phonegap-plugins.git
synced 2026-04-24 03:00:11 -04:00
116 lines
3.1 KiB
HTML
116 lines
3.1 KiB
HTML
<!DOCTYPE HTML>
|
|
<html>
|
|
|
|
<head>
|
|
<title>Cordova BluetoothPlugin example</title>
|
|
|
|
<script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
|
|
<script type="text/javascript" charset="utf-8" src="bluetooth.js"></script>
|
|
<script type="text/javascript" charset="utf-8" src="jquery-1.6.4.js"></script>
|
|
|
|
<script type="text/javascript" charset="utf-8">
|
|
var g_socketid = -1;
|
|
|
|
window.addEventListener('load', function () {
|
|
document.addEventListener('deviceready', function () {
|
|
alert("Cordova is now loaded!");
|
|
window.plugins = {
|
|
BluetoothPlugin: cordova.require( 'cordova/plugin/bluetooth' )
|
|
};
|
|
}, false);
|
|
}, false);
|
|
|
|
function enableBT() {
|
|
window.plugins.BluetoothPlugin.enable( function() {
|
|
alert( 'Enabling successfull' );
|
|
}, function(error) {
|
|
alert( 'Error enabling BT: ' + error );
|
|
} );
|
|
}
|
|
|
|
function disableBT() {
|
|
window.plugins.BluetoothPlugin.disable( function() {
|
|
alert( 'Disabling successfull' );
|
|
}, function(error) {
|
|
alert( 'Error disabling BT: ' + error );
|
|
} );
|
|
}
|
|
|
|
function discoverDevices() {
|
|
window.plugins.BluetoothPlugin.discoverDevices( function(devices) {
|
|
$('#bt-devices-select').html('');
|
|
|
|
for( var i = 0; i < devices.length; i++ ) {
|
|
$('#bt-devices-select').append( $( '<option value="' + devices[i].address + '">' + devices[i].name + '</option>' ) );
|
|
}
|
|
}, function(error) { alert( 'Error: ' + error ); } );
|
|
}
|
|
|
|
function listUUIDs() {
|
|
window.plugins.BluetoothPlugin.getUUIDs( function(uuids) {
|
|
$('#bt-device-uuids').html('');
|
|
|
|
for( var i = 0; i < uuids.length; i++ ) {
|
|
$('#bt-device-uuids').append( $( '<option value="' + uuids[i] + '">' + uuids[i] + '</option>' ) );
|
|
}
|
|
}, function(error) { alert( 'Error: ' + error ); }, $( '#bt-devices-select' ).val() );
|
|
}
|
|
|
|
function openRfcomm() {
|
|
window.plugins.BluetoothPlugin.connect(
|
|
function(socketId) {
|
|
g_socketid = socketId;
|
|
console.log( 'Socket-id: ' + g_socketid );
|
|
},
|
|
function(error) {
|
|
alert( 'Error: ' + error );
|
|
},
|
|
$( '#bt-devices-select' ).val(),
|
|
$( '#bt-device-uuids' ).val()
|
|
);
|
|
}
|
|
|
|
function readRfcomm() {
|
|
window.plugins.BluetoothPlugin.read( bp_readSuccess, bp_readError, g_socketid );
|
|
}
|
|
|
|
function bp_readError( error ) {
|
|
alert( 'Error: ' + error );
|
|
}
|
|
|
|
function bp_readSuccess( p_data ) {
|
|
$('#bt-data-dump').append( '<br>' + p_data );
|
|
|
|
// Continue reading...
|
|
window.plugins.BluetoothPlugin.read( bp_readSuccess, bp_readError, g_socketid );
|
|
|
|
return;
|
|
}
|
|
|
|
</script>
|
|
</head>
|
|
|
|
<body>
|
|
<button onclick="enableBT();">Enable</button>
|
|
<button onclick="disableBT();">Disable</button>
|
|
<br />
|
|
<button onclick="discoverDevices();">Discover</button>
|
|
<br />
|
|
<select id='bt-devices-select'>
|
|
</select>
|
|
<br />
|
|
<button id='bt-pair' onclick="listUUIDs();">List UUIDs</button>
|
|
</p>
|
|
<p>
|
|
<select id='bt-device-uuids'>
|
|
</select>
|
|
<br />
|
|
<button id='bt-connect' onclick="openRfcomm();">Connect</button>
|
|
<br />
|
|
<button onclick="readRfcomm();">Read</button>
|
|
<br />
|
|
<div id='bt-data-dump'></div>
|
|
</p>
|
|
</body>
|
|
</html>
|