### Install BLE Peripheral Plugin with Cordova CLI (Shell) Source: https://github.com/don/cordova-plugin-ble-peripheral/blob/master/README.md Installs the Bluetooth Low Energy (BLE) Peripheral plugin for Apache Cordova using the Cordova command-line interface. This command should be run within your Cordova project directory. ```shell $ cordova plugin add cordova-plugin-ble-peripheral ``` -------------------------------- ### Install BLE Peripheral Plugin with PhoneGap CLI (Shell) Source: https://github.com/don/cordova-plugin-ble-peripheral/blob/master/README.md Installs the Bluetooth Low Energy (BLE) Peripheral plugin for PhoneGap applications using the PhoneGap command-line interface. Execute this command in your PhoneGap project directory. ```shell $phonegap plugin add cordova-plugin-ble-peripheral ``` -------------------------------- ### Monitor Bluetooth State Changes (JavaScript) Source: https://context7.com/don/cordova-plugin-ble-peripheral/llms.txt This code registers a callback function to be executed whenever the device's Bluetooth state changes. It logs the current state and provides examples for handling 'on' and 'off' states, which is crucial for determining when to start advertising services. ```javascript // Register callback for Bluetooth state changes blePeripheral.onBluetoothStateChange(function(state) { console.log('Bluetooth State is:', state); if (state === 'on') { console.log('Bluetooth is enabled, can start advertising'); // Safe to create services and start advertising } else if (state === 'off') { console.log('Bluetooth is disabled'); // Handle Bluetooth disabled state } document.getElementById('status').textContent = 'Bluetooth: ' + state; }); // Possible states: 'on', 'off', 'unauthorized', 'unsupported' // This callback is automatically registered when the plugin loads ``` -------------------------------- ### Defining and Creating BLE Service with JSON (JavaScript) Source: https://github.com/don/cordova-plugin-ble-peripheral/blob/master/README.md Defines a Bluetooth Service using a JSON object, specifying its UUID and characteristics. Characteristics can have properties, permissions, and descriptors. The service is then created and advertising is started using this JSON definition. ```javascript var uartService = { uuid: SERVICE_UUID, characteristics: [ { uuid: TX_UUID, properties: property.WRITE, permissions: permission.WRITEABLE, descriptors: [ { uuid: '2901', value: 'Transmit' } ] }, { uuid: RX_UUID, properties: property.READ | property.NOTIFY, permissions: permission.READABLE, descriptors: [ { uuid: '2901', value: 'Receive' } ] } ] }; Promise.all([ blePeripheral.createServiceFromJSON(uartService), blePeripheral.startAdvertising(uartService.uuid, 'UART') ]).then( function() { console.log ('Created UART Service'); }, app.onError ); ``` -------------------------------- ### Install BLE Peripheral Plugin in PhoneGap Build (XML) Source: https://github.com/don/cordova-plugin-ble-peripheral/blob/master/README.md Adds the Bluetooth Low Energy (BLE) Peripheral plugin to a PhoneGap Build project by editing the `config.xml` file. This ensures the plugin is included when the application is built via the PhoneGap Build service. ```xml ``` -------------------------------- ### Create Flashlight Control Service (JavaScript) Source: https://context7.com/don/cordova-plugin-ble-peripheral/llms.txt This snippet illustrates the creation of a custom BLE service ('Flashlight Control') with two characteristics: 'Switch' and 'Dimmer'. It uses `createServiceFromJSON` and `startAdvertising`, and includes an `onWriteRequest` handler to interact with the device's flashlight using `window.plugins.flashlight`. ```javascript var SERVICE_UUID = 'FF10'; var SWITCH_UUID = 'FF11'; var DIMMER_UUID = 'FF12'; var property = blePeripheral.properties; var permission = blePeripheral.permissions; // Define flashlight control service var flashlightService = { uuid: SERVICE_UUID, characteristics: [ { uuid: SWITCH_UUID, properties: property.WRITE | property.READ, permissions: permission.WRITEABLE | permission.READABLE, descriptors: [ { uuid: '2901', value: 'Switch' } ] }, { uuid: DIMMER_UUID, properties: property.WRITE | property.READ, permissions: permission.WRITEABLE | permission.READABLE, descriptors: [ { uuid: '2901', value: 'Dimmer' } ] } ] }; // Create and advertise service Promise.all([ blePeripheral.createServiceFromJSON(flashlightService), blePeripheral.startAdvertising(flashlightService.uuid, 'Flashlight') ]).then( function() { console.log('Created Flashlight Service'); }, function(error) { console.error('Error:', error); } ); // Handle write requests to control flashlight blePeripheral.onWriteRequest(function(request) { if (request.characteristic === SWITCH_UUID) { var data = new Uint8Array(request.value); if (data[0] === 0) { window.plugins.flashlight.switchOff(); } else { window.plugins.flashlight.switchOn(); } } if (request.characteristic === DIMMER_UUID) { var data = new Uint8Array(request.value); var brightness = data[0] / 255.0; // Convert byte to 0.0-1.0 window.plugins.flashlight.switchOn( function() { console.log('Brightness set to', brightness); }, function() { console.log('Failed to set brightness'); }, { intensity: brightness } ); } }); // Output: Created Flashlight Service // Central devices can now control flashlight via BLE ``` -------------------------------- ### Create BLE Service Programmatically Source: https://context7.com/don/cordova-plugin-ble-peripheral/llms.txt Builds a BLE service step-by-step using individual API calls for granular control over service and characteristic creation. This method is useful for simpler service definitions or when dynamic creation is needed. It returns a Promise that resolves upon successful creation and advertising. ```javascript var SERVICE_UUID = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'; var TX_UUID = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'; var RX_UUID = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'; var property = blePeripheral.properties; var permission = blePeripheral.permissions; // Create service with characteristics programmatically Promise.all([ blePeripheral.createService(SERVICE_UUID), blePeripheral.addCharacteristic(SERVICE_UUID, TX_UUID, property.WRITE, permission.WRITEABLE), blePeripheral.addCharacteristic(SERVICE_UUID, RX_UUID, property.READ | property.NOTIFY, permission.READABLE), blePeripheral.publishService(SERVICE_UUID), blePeripheral.startAdvertising(SERVICE_UUID, 'UART') ]).then( function() { console.log('Created UART Service'); }, function(error) { console.error('Error:', error); } ); // Output: Created UART Service // Note: Descriptors are only supported with JSON format in version 1.0 ``` -------------------------------- ### Create BLE Service using JSON Configuration Source: https://context7.com/don/cordova-plugin-ble-peripheral/llms.txt Defines a complete BLE service structure, including characteristics and descriptors, using a JSON configuration object. This method is suitable for complex service definitions and requires the `blePeripheral` object to be available. It returns a Promise that resolves when the service is created and advertising begins. ```javascript var property = blePeripheral.properties; var permission = blePeripheral.permissions; var uartService = { uuid: '6E400001-B5A3-F393-E0A9-E50E24DCCA9E', characteristics: [ { uuid: '6E400002-B5A3-F393-E0A9-E50E24DCCA9E', properties: property.WRITE, permissions: permission.WRITEABLE, descriptors: [ { uuid: '2901', value: 'Transmit' } ] }, { uuid: '6E400003-B5A3-F393-E0A9-E50E24DCCA9E', properties: property.READ | property.NOTIFY, permissions: permission.READABLE, descriptors: [ { uuid: '2901', value: 'Receive' } ] } ] }; // Create service and start advertising Promise.all([ blePeripheral.createServiceFromJSON(uartService), blePeripheral.startAdvertising(uartService.uuid, 'UART') ]).then( function() { console.log('Created UART Service'); }, function(error) { console.error('Error:', error); } ); // Output: Created UART Service // Device now advertising as 'UART' with Nordic UART service ``` -------------------------------- ### Accessing BLE Characteristic Properties and Permissions (JavaScript) Source: https://context7.com/don/cordova-plugin-ble-peripheral/llms.txt Demonstrates how to access and log characteristic properties (READ, WRITE, NOTIFY, etc.) and permissions (READABLE, WRITEABLE, etc.) using the Cordova BLE Peripheral plugin. It also shows how to combine these using bitwise OR operations. Note that permissions are automatically adjusted for platform differences. ```javascript var properties = blePeripheral.properties; console.log('READ:', properties.READ); // 0x02 console.log('WRITE:', properties.WRITE); // 0x08 console.log('WRITE_NO_RESPONSE:', properties.WRITE_NO_RESPONSE); // 0x04 console.log('NOTIFY:', properties.NOTIFY); // 0x10 console.log('INDICATE:', properties.INDICATE); // 0x20 var permissions = blePeripheral.permissions; console.log('READABLE:', permissions.READABLE); // 0x01 console.log('WRITEABLE:', permissions.WRITEABLE); // iOS: 0x02, Android: 0x10 console.log('READ_ENCRYPTION_REQUIRED:', permissions.READ_ENCRYPTION_REQUIRED); // iOS: 0x04, Android: 0x02 console.log('WRITE_ENCRYPTION_REQUIRED:', permissions.WRITE_ENCRYPTION_REQUIRED); // iOS: 0x08, Android: 0x20 // Combine properties using bitwise OR var readNotify = properties.READ | properties.NOTIFY; var readWrite = properties.READ | properties.WRITE; var writeableReadable = permissions.WRITEABLE | permissions.READABLE; // Note: Permissions are automatically adjusted for platform differences ``` -------------------------------- ### Creating BLE Service Programmatically (JavaScript) Source: https://github.com/don/cordova-plugin-ble-peripheral/blob/master/README.md Creates a Bluetooth Service and its characteristics programmatically without using JSON. This method allows for dynamic service creation. Note that descriptor support is limited to the JSON format in version 1.0. ```javascript Promise.all([ blePeripheral.createService(SERVICE_UUID), blePeripheral.addCharacteristic(SERVICE_UUID, TX_UUID, property.WRITE, permission.WRITEABLE), blePeripheral.addCharacteristic(SERVICE_UUID, RX_UUID, property.READ | property.NOTIFY, permission.READABLE), blePeripheral.publishService(SERVICE_UUID), blePeripheral.startAdvertising(SERVICE_UUID, 'UART') ]).then( function() { console.log ('Created UART Service'); }, app.onError ); ``` -------------------------------- ### Registering BLE Peripheral Callbacks (JavaScript) Source: https://github.com/don/cordova-plugin-ble-peripheral/blob/master/README.md Register callback functions to receive notifications from the BLE peripheral plugin. This includes events like write requests and Bluetooth state changes. Ensure the application object has the corresponding handler functions defined. ```javascript blePeripheral.onWriteRequest(app.didReceiveWriteRequest); blePeripheral.onBluetoothStateChange(app.onBluetoothStateChange); ``` -------------------------------- ### Handle Write Requests from Central Devices Source: https://context7.com/don/cordova-plugin-ble-peripheral/llms.txt Registers a callback function to process data written by central BLE devices to characteristics. The callback receives a `request` object containing the service, characteristic UUIDs, and the received `value` as an ArrayBuffer. Helper functions `stringToBytes` and `bytesToString` are provided for data conversion. ```javascript function stringToBytes(string) { var array = new Uint8Array(string.length); for (var i = 0; i < string.length; i++) { array[i] = string.charCodeAt(i); } return array.buffer; } function bytesToString(buffer) { return String.fromCharCode.apply(null, new Uint8Array(buffer)); } // Register callback for write requests blePeripheral.onWriteRequest(function(request) { // request object contains: service, characteristic, value (ArrayBuffer) var message = bytesToString(request.value); console.log('Received:', message); console.log('Service:', request.service); console.log('Characteristic:', request.characteristic); // Process the received data document.getElementById('output').innerHTML += message + '
'; }); // Example request object received: // { // service: '6E400001-B5A3-F393-E0A9-E50E24DCCA9E', // characteristic: '6E400002-B5A3-F393-E0A9-E50E24DCCA9E', // value: ArrayBuffer(5) // contains "Hello" // } ``` -------------------------------- ### Update Characteristic Value and Notify Subscribers (JavaScript) Source: https://context7.com/don/cordova-plugin-ble-peripheral/llms.txt This snippet demonstrates how to update a BLE characteristic's value and automatically notify subscribed central devices. It requires the `blePeripheral` object and a helper function `stringToBytes`. The value must be an ArrayBuffer. ```javascript var SERVICE_UUID = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'; var RX_UUID = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'; function stringToBytes(string) { var array = new Uint8Array(string.length); for (var i = 0; i < string.length; i++) { array[i] = string.charCodeAt(i); } return array.buffer; } // Update characteristic value and notify subscribers var message = 'Hello from peripheral'; var bytes = stringToBytes(message); blePeripheral.setCharacteristicValue(SERVICE_UUID, RX_UUID, bytes) .then( function() { console.log('Updated RX value to:', message); console.log('Notification sent to subscribed centrals'); }, function(error) { console.error('Error updating RX value:', error); } ); // Output: Updated RX value to: Hello from peripheral // Note: Value must be an ArrayBuffer, other types will be rejected ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.