- Saved searches
- Use saved searches to filter your results more quickly
- License
- simondlevy/AndroidBluetoothClient
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- bleak 0.20.2
- Installation
- Features
- Usage
- Android Bluetooth Low Energy¶
- Quick start development environment¶
- Build¶
- Build with a local version¶
- Contributors¶
- API¶
- Client¶
- BluetoothDispatcher¶
- Decorators¶
- Advertisement¶
- Services¶
- Constants¶
- Permissions¶
- Scan settings¶
- Scan filters¶
- Advertising¶
- Advertiser¶
- Payload¶
- Constants¶
- Usage Examples¶
- Alert¶
- Change MTU¶
- Scan settings¶
- Scan filters¶
- Adapter state¶
- Advertising¶
- Advertise with data and additional (scannable) data¶
- Set and advertise device name¶
- Battery service data¶
- Use iBeacon advertising format¶
- Android Services¶
- BLE devices scanning service¶
- Advertising service¶
- tito / bluetooth.py
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Simple Bluetooth socket client for Android
License
simondlevy/AndroidBluetoothClient
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
- DeviceListActivity: the main (first) activity of your app. It lists the Bluetooth devices with which your Android device is paired and connects to the device you select.
- CommunicationsTask: an asynchronous task (i.e., thread) for managing communications with the device to which you’ve connected.
- CommunicationsActivity: an abstract class that supports reading from and writing to the server via its mBluetoothConnection object.
An example subclass, MyCommunicationsActivity, provides a seek-bar (slider) and text widget to send and receive values from your server.
The easiest way to try out this app is with the Python Bluetooth server that we developed to work with it. This server uses the same simple protocol (period-delimited messages) as the code in the MyCommunicationsActivity class.
About
Simple Bluetooth socket client for Android
bleak 0.20.2
Bleak is an acronym for Bluetooth Low Energy platform Agnostic Klient.
Bleak is a GATT client software, capable of connecting to BLE devices acting as GATT servers. It is designed to provide a asynchronous, cross-platform Python API to connect and communicate with e.g. sensors.
Installation
Features
- Supports Windows 10, version 16299 (Fall Creators Update) or greater
- Supports Linux distributions with BlueZ >= 5.43
- OS X/macOS support via Core Bluetooth API, from at least OS X version 10.11
- Android backend compatible with python-for-android
Bleak supports reading, writing and getting notifications from GATT servers, as well as a function for discovering BLE devices.
Usage
To discover Bluetooth devices that can be connected to:
Connect to a Bluetooth device and read its model number:
DO NOT NAME YOUR SCRIPT bleak.py ! It will cause a circular import error.
See examples folder for more code, for instance example code for connecting to a TI SensorTag CC2650
Android Bluetooth Low Energy¶
Python interface to Android Bluetooth Low Energy API.
Code repository: | |
---|---|
https://github.com/b3b/able | |
Documentation: | https://herethere.me/able |
Changelog: | https://github.com/b3b/able/blob/master/CHANGELOG.rst |
Quick start development environment¶
able is included in PythonHere app, together with the Jupyter Notebook it could be used as a development environment.
Build¶
The following instructions are for building app with buildozer tool.
able_recipe recipe should be added to buildozer.spec requirements:
requirements = python3,kivy,android,able_recipe
Bluetooth permissions should be requested in buildozer.spec:
android.permissions = BLUETOOTH, BLUETOOTH_ADMIN, BLUETOOTH_SCAN, BLUETOOTH_CONNECT, BLUETOOTH_ADVERTISE, ACCESS_FINE_LOCATION
App configuration example: buildozer.spec
Build with a local version¶
To build app with a local (modified) version of able,
path to able recipes directory should be set in buildozer.spec:
p4a.local_recipes = /path/to/cloned/repo/recipes
Contributors¶
API¶
Client¶
BluetoothDispatcher¶
Decorators¶
Advertisement¶
Services¶
Constants¶
Permissions¶
Scan settings¶
>>> settings = ScanSettingsBuilder() \ . .setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE) \ . .setCallbackType( . ScanSettings.CALLBACK_TYPE_FIRST_MATCH | . ScanSettings.CALLBACK_TYPE_MATCH_LOST . )
Scan filters¶
Advertising¶
Advertiser¶
Payload¶
Constants¶
Usage Examples¶
Alert¶
Change MTU¶
Scan settings¶
from able import BluetoothDispatcher from able.scan_settings import ScanSettingsBuilder, ScanSettings # Use faster detection (more power usage) mode settings = ScanSettingsBuilder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) BluetoothDispatcher().start_scan(settings=settings)
Scan filters¶
from able import BluetoothDispatcher from able.filters import ( DeviceAddressFilter, DeviceNameFilter, ManufacturerDataFilter, ServiceDataFilter, ServiceUUIDFilter ) ble = BluetoothDispatcher() # Start scanning with the condition that device has one of names: "Device1" or "Device2" ble.start_scan(filters=[DeviceNameFilter("Device1"), DeviceNameFilter("Device2")]) ble.stop_scan() # Start scanning with the condition that # device advertises "180f" service and one of names: "Device1" or "Device2" ble.start_scan(filters=[ ServiceUUIDFilter('0000180f-0000-1000-8000-00805f9b34fb') & DeviceNameFilter("Device1"), ServiceUUIDFilter('0000180f-0000-1000-8000-00805f9b34fb') & DeviceNameFilter("Device2") ])
Adapter state¶
Advertising¶
Advertise with data and additional (scannable) data¶
from able import BluetoothDispatcher from able.advertising import ( Advertiser, AdvertiseData, ManufacturerData, Interval, ServiceUUID, ServiceData, TXPower, ) advertiser = Advertiser( ble=BluetoothDispatcher(), data=AdvertiseData(ServiceUUID("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")), scan_data=AdvertiseData(ManufacturerData(id=0xAABB, data=b"some data")), interval=Interval.MEDIUM, tx_power=TXPower.MEDIUM, ) advertiser.start()
Set and advertise device name¶
from able import BluetoothDispatcher from able.advertising import Advertiser, AdvertiseData, DeviceName ble = BluetoothDispatcher() ble.name = "New test device name" # There must be a wait and check, it takes time for new name to take effect print(f"New device name is set: ble.name>") Advertiser( ble=ble, data=AdvertiseData(DeviceName()) )
Battery service data¶
Use iBeacon advertising format¶
import uuid from able import BluetoothDispatcher from able.advertising import Advertiser, AdvertiseData, ManufacturerData data = AdvertiseData( ManufacturerData( 0x4C, # Apple Manufacturer ID bytes([ 0x2, # SubType: Custom Manufacturer Data 0x15 # Subtype lenth ]) + uuid.uuid4().bytes + # UUID of beacon bytes([ 0, 15, # Major value 0, 1, # Minor value 10 # RSSI, dBm at 1m ])) ) Advertiser(BluetoothDispatcher(), data).start()
Android Services¶
BLE devices scanning service¶
Advertising service¶
tito / bluetooth.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
»’ |
Bluetooth/Pyjnius example |
========================= |
This was used to send some bytes to an arduino via bluetooth. |
The app must have BLUETOOTH and BLUETOOTH_ADMIN permissions (well, i didn’t |
tested without BLUETOOTH_ADMIN, maybe it works.) |
Connect your device to your phone, via the bluetooth menu. After the |
pairing is done, you’ll be able to use it in the app. |
»’ |
from jnius import autoclass |
BluetoothAdapter = autoclass ( ‘android.bluetooth.BluetoothAdapter’ ) |
BluetoothDevice = autoclass ( ‘android.bluetooth.BluetoothDevice’ ) |
BluetoothSocket = autoclass ( ‘android.bluetooth.BluetoothSocket’ ) |
UUID = autoclass ( ‘java.util.UUID’ ) |
def get_socket_stream ( name ): |
paired_devices = BluetoothAdapter . getDefaultAdapter (). getBondedDevices (). toArray () |
socket = None |
for device in paired_devices : |
if device . getName () == name : |
socket = device . createRfcommSocketToServiceRecord ( |
UUID . fromString ( «00001101-0000-1000-8000-00805F9B34FB» )) |
recv_stream = socket . getInputStream () |
send_stream = socket . getOutputStream () |
break |
socket . connect () |
return recv_stream , send_stream |
if __name__ == ‘__main__’ : |
recv_stream , send_stream = get_socket_stream ( ‘linvor’ ) |
send_stream . write ( ‘hello \n ‘ ) |
send_stream . flush () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# Same as before, with a kivy-based UI |
»’ |
Bluetooth/Pyjnius example |
========================= |
This was used to send some bytes to an arduino via bluetooth. |
The app must have BLUETOOTH and BLUETOOTH_ADMIN permissions (well, i didn’t |
tested without BLUETOOTH_ADMIN, maybe it works.) |
Connect your device to your phone, via the bluetooth menu. After the |
pairing is done, you’ll be able to use it in the app. |
»’ |
from jnius import autoclass |
BluetoothAdapter = autoclass ( ‘android.bluetooth.BluetoothAdapter’ ) |
BluetoothDevice = autoclass ( ‘android.bluetooth.BluetoothDevice’ ) |
BluetoothSocket = autoclass ( ‘android.bluetooth.BluetoothSocket’ ) |
UUID = autoclass ( ‘java.util.UUID’ ) |
def get_socket_stream ( name ): |
paired_devices = BluetoothAdapter . getDefaultAdapter (). getBondedDevices (). toArray () |
socket = None |
for device in paired_devices : |
if device . getName () == name : |
socket = device . createRfcommSocketToServiceRecord ( |
UUID . fromString ( «00001101-0000-1000-8000-00805F9B34FB» )) |
recv_stream = socket . getInputStream () |
send_stream = socket . getOutputStream () |
break |
socket . connect () |
return recv_stream , send_stream |
if __name__ == ‘__main__’ : |
kv = »’ |
BoxLayout: |
Button: |
text: ‘0’ |
on_release: app.reset([b1, b2, b3, b4, b5]) |
ToggleButton: |
id: b1 |
text: ‘1’ |
on_release: app.send(self.text) |
ToggleButton: |
id: b2 |
text: ‘2’ |
on_release: app.send(self.text) |
ToggleButton: |
id: b3 |
text: ‘3’ |
on_release: app.send(self.text) |
ToggleButton: |
id: b4 |
text: ‘4’ |
on_release: app.send(self.text) |
ToggleButton: |
id: b5 |
text: ‘5’ |
on_release: app.send(self.text) |
»’ |
from kivy . lang import Builder |
from kivy . app import App |
class Bluetooth ( App ): |
def build ( self ): |
self . recv_stream , self . send_stream = get_socket_stream ( ‘linvor’ ) |
return Builder . load_string ( kv ) |
def send ( self , cmd ): |
self . send_stream . write ( ‘<> \n ‘ . format ( cmd )) |
self . send_stream . flush () |
def reset ( self , btns ): |
for btn in btns : |
btn . state = ‘normal’ |
self . send ( ‘0 \n ‘ ) |
Bluetooth (). run () |