- How can Android communicate with Bluetooth devices?
- How can Android communicate with Bluetooth devices?
- What is the purpose of nearby device scanning?
- How do I stop my phone from casting to my TV?
- How do I stop other devices from casting on my TV?
- What does it mean when it says a device on your WiFi is casting?
- Using Web Bluetooth to communicate with Bluetooth devices
- What is BLE?
- Generic Access Profile (GAP)
- Generic Attribute Profile (GATT)
- Services
- Characteristics
- Our Use Case
- Hardware required
- Software required
- Walkthrough
- Setup the balena device
- Deploy the project code
- Set up Raspberry Pi
- Load the Web Bluetooth App
- Going Further
- Wrap Up
- Communicate with Android bluetooth device via NodeJS and Termux
- 1 Answer 1
How can Android communicate with Bluetooth devices?
How can Android communicate with Bluetooth devices?
- BLE peripheral can only be connected to one central device at a time.
- The Server does not send data on its own, but only when a Client requests so.
- Notifications and Indications are operations initiated by the Server whenever a value in the database changes.
Can someone connect to your Bluetooth?
If you keep Wi-Fi and Bluetooth active, hackers can see what networks you’ve connected to before, spoof them and trick your phone into connecting to Wi-Fi and Bluetooth devices that hackers carry around. Once connected to your phone, hackers can bombard your device with malware, steal data or spy on you.
What is the purpose of nearby device scanning?
Google is said to be working on a new Android feature that allows phones and tablets to automatically connect with devices and places very close around them, according to Android Police. The feature, called Nearby, might allow people to easily share information with others or receive deals from stores that they’re in.
How do I stop my phone from casting to nearby devices?
Turn off Cast media control notifications on your phone
- On your phone, tap Settings .
- Tap Google Devices & sharing Cast options Turn off Media controls for Cast devices.
How do I stop my phone from casting to my TV?
Step 4. Stop casting your screen from your Android device
- Make sure your mobile phone or tablet is on the same Wi-Fi network as your Chromecast device.
- Open the Google Home app .
- Tap your device. Stop mirroring .
How do I stop people pairing with my TV?
Put the TV on a different subnet. Breaks most casting. As far as I know they need to be on the same wifi as your TV to do this, the only way you can stop it is if you have the control when they are accessing the TV you just deny them.
How do I stop other devices from casting on my TV?
1. Prevent Others from Controlling Your Chromecast Device
- Tap on your Chromecast device.
- On the next screen, tap on Chromecast Settings icon located at the top right corner of your screen.
- On Device Settings screen, scroll down and disable Let others control your cast media option.
What does it mean a device on your WiFi is casting?
Google added an Android update that allows all users on the same WiFi network to see when you’re casting. The update automatically notifies them and grants them control over your cast.
What does it mean when it says a device on your WiFi is casting?
It generally will mean that something that is connected to your network, like your phone, is broadcasting video or audio to another device on the network, such as your television or speakers.
How do I stop my Samsung TV from casting to other devices?
Here’s how you can disable Shared Controls:
- Step 1: Power on your Android TV and Go to Google Home App.
- Step 2: Select your Android TV or Chromecast and tap on the settings wheel.
- Step 3: Now, Set ‘Let others control your Cast Media’ to ‘Never’
Using Web Bluetooth to communicate with Bluetooth devices
Adding Bluetooth to your IoT project usually means developing native applications to interact with your devices. However, the new Web Bluetooth API specification allows web browsers to communicate directly with Bluetooth Low Energy devices. While the specification is not finalized yet, we can already experiment with it using Chrome on macOS, Linux, Windows, and Android.
In this article, we will demonstrate how to use bleno — a Node.js library, to implement Bluetooth Low Energy (BLE) peripherals on a balenaOS-powered device and use a web app to communicate with it from a browser.
But first, let’s understand some basic BLE concepts. Or, skip ahead if you are familiar with BLE and want to get to the project.
What is BLE?
Bluetooth Low Energy (BLE), formerly known as Bluetooth Smart, is a subset of the classic Bluetooth specification designed to provide communication with low power consumption. It is helpful to understand some specific concepts and terminology when developing BLE applications.
Generic Access Profile (GAP)
The GAP defines how BLE devices communicate with other BLE-enabled devices. In the context of a BLE network, a device can be:
- A peripheral device: these are typically low power and resource-constrained devices that advertise themselves and wait for a central device to connect to them. The Raspberry Pi acts as a peripheral device in this project.
- A central device: these are typically smartphones or powerful devices that initiate a connection with peripheral BLE devices. For our demo, a laptop or an android smartphone with the latest Chrome web browser will act as the central device. The Web Bluetooth application loaded through the browser initiates a connection with the Raspberry Pi.
A peripheral device can connect to only a single central device at a time. Once a connection has been established, peripheral devices stop advertising themselves. A central device, on the other hand, can connect to several peripheral devices and can relay messages among peripheral devices.
Generic Attribute Profile (GATT)
Once BLE devices have established a connection, the Generic Attribute Profile (GATT) dictates how data is exchanged over the connection. Devices can adopt different roles:
- GATT Server: These are typically peripheral devices that have attributes that the client can make requests to.
- GATT Client: Send requests to the GATT server. They can read or write to attributes on the server.
Services
A GATT server offers one or more services. Some services can be grouped into profiles. There are several predefined profiles and services by the Bluetooth SIG. For example, the Blood Pressure Profile contains the Blood Pressure Service and the Device Information Service.
Each service is identified by a Universally Unique Identifier (UUID). For officially adopted BLE Services, the UUID is 16-bit, while custom services are 128-bit. A service like Online UUID Generator or UUIDTools.com can be used to generate custom UUIDs.
Characteristics
Services, in turn, contain one or more characteristics. They are also identified by either 16-bit or 128-bit UUIDs. A Characteristic has a value, which is an array of bytes and a collection of Descriptors.
Depending on the properties of the Characteristic, a GATT Client can read or write its value, or register to be notified when the value changes. A GATT Descriptor provides further information about a Characteristic’s value.
Our Use Case
For the purpose of our demo, we are going to use Bluetooth to control an LED connected to a Raspberry Pi. A physical push button is also attached to the Raspberry Pi and allows toggling the LED on and off. Our Web Bluetooth application will be able to receive notifications when the physical button is pressed.
The Raspberry Pi will be configured as a peripheral device called balenaBLE with the following services and characteristics:
Hardware required
- A balenaOS supported device with BLE e.g Raspberry Pi A+/3B/3B+/Zero W
- An SD card
- Power supply
- LED
- Resistor (above 50 Ohms)
- A pushbutton
- A laptop or Android smartphone with BLE running the latest Chrome web browser
Software required
- balenaEtcher to write balenaOS to an SD card
- The balena-web-ble project from Github
- balena CLI tool — to be installed on your computer, allowing you to install the project code on the balena device
Walkthrough
Setup the balena device
- Sign up for a free balenaCloud account
- Create a balenaCloud application
- Add a device and download the balenaOS disk image from the dashboard
- Flash your SD card with the balenaOS disk image and boot the device
- After a few seconds, it should connect to the internet and show up on the balenaCloud dashboard.
Deploy the project code
Once the device is connected and showing in the dashboard, it is time to deploy the application that will create a BLE peripheral device.
- Download the project from ~~ https://github.com/balena-io-playground/balena-web-ble/ ~~ https://github.com/balenalabs-incubator/balena-web-ble and unzip the file. If you are familiar with git, you can also use git clone
- Using the above-mentioned balenaCLI, run the command balena push where is the name of the application you created within the balenaCloud dashboard earlier. For this example project, we will use the balena push web-ble command.
If all went well you’ll see the balena unicorn mascot and the code you’ve just pushed will automatically be distributed to the devices in your application.
You should also see the main service running on the dashboard:
Set up Raspberry Pi
By default, the project uses GPIO 17 for the LED and GPIO 4 for the pushbutton . If you want to use a different pin, you can configure the LED_PIN and BTN_PIN environment variables in the dashboard. For example, if you want to use GPIO 27 for the LED instead:
Load the Web Bluetooth App
You can download and run the web app yourself from the repo or you can try our hosted version here.
Load the web app on your laptop or android device in the Chrome browser.
Turn on Bluetooth on your smartphone or laptop. Click on the “Connect” button which will trigger device scanning. After connecting, you can see the device’s CPU information and will be able to toggle the LED on or off.
You can also see the device logs in the dashboard as the light is toggled.
You can also use the pushbutton to control the LED. The Web Bluetooth application will be notified and reflect the actual state of the LED on the UI.
The Web Bluetooth API is available in secure contexts only in the web browser. For development purposes, the Web Bluetooth is available from the localhost . The web app here has been deployed on Github Pages which provides HTTPS access by default.
Going Further
This project uses the bleno library to create a BLE peripheral device. Feel free to explore the API and experiment with other BLE properties and characteristics to make your own custom BLE peripheral device.
You can then customize the web app accordingly to suit your use case. The relevant files are index.html and ble-client.js in the project.
Wrap Up
In this post, we set up a Raspberry Pi as a Bluetooth Low Energy peripheral device and used Web Bluetooth to control it from a web app. This project can easily be adapted to suit your use case. The following resources might be helpful when developing your own BLE application:
Communicate with Android bluetooth device via NodeJS and Termux
We are looking to communicate with a bluetooth device connected to an Android tablet. We are using Termux and have NodeJS installed. Does anyone know if it is even possible to communicate with a bluetooth device? Would we have to attempt to communicate with the device directly through the /dev folder? It is my understanding that Android is built on top of the Linux kernel, however, it has implemented specific things on top of it to interact for other things such as connectivity. Would the device even be accessible through the /dev folder via NodejS «serialport» or another tool? As a last resort, if this is not possible, I guess we could try to build NodeJS in the Android OS through a rooted terminal. I’ve heard this isn’t as easy as one would think though. Through Termux I am able to access the /dev folder and see all the devices. Not sure how the permission would work though. Thanks.
1 Answer 1
You can communicate through the serial port using this tool. I have never use this tool but providing this only as a reference, since android is built on a linux kernel this might work. Please note that the examples are same as documentation.
var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort(); btSerial.on('found', function(address, name) < btSerial.findSerialPortChannel(address, function(channel) < btSerial.connect(address, channel, function() < console.log('connected'); btSerial.write(new Buffer('my data', 'utf-8'), function(err, bytesWritten) < if (err) console.log(err); >); btSerial.on('data', function(buffer) < console.log(buffer.toString('utf-8')); >); >, function () < console.log('cannot connect'); >); // close the connection when you're ready btSerial.close(); >, function() < console.log('found nothing'); >); >); btSerial.inquire();
Basic server usage (only on Linux)
var server = new(require('bluetooth-serial-port')).BluetoothSerialPortServer(); var CHANNEL = 10; // My service channel. Defaults to 1 if omitted. var UUID = '38e851bc-7144-44b4-9cd8-80549c6f2912'; // My own service UUID. Defaults to '1101' if omitted server.listen(function (clientAddress) < console.log('Client: ' + clientAddress + ' connected!'); server.on('data', function(buffer) < console.log('Received data from client: ' + buffer); // . console.log('Sending data to the client'); server.write(new Buffer('. '), function (err, bytesWritten) < if (err) < console.log('Error!'); >else < console.log('Send ' + bytesWritten + ' to the client!'); >>); >); >, function(error)< console.error("Something wrong happened!:" + error); >, );