GithubHelp home page GithubHelp logo

ve3344 / bleadvertisement Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 2.0 326 KB

A library to broadcast bluetooth as the host to on linux, which can work on the Raspberry Pi

License: MIT License

Java 100.00%
bluetooth bluetooth-low-energy dbus bluez bluetooth-le

bleadvertisement's Introduction

一个Linux下作为中心节点广播低功耗蓝牙的库。

可以在Raspberry Pi上使用

实现功能

  • 广播蓝牙
  • 设置蓝牙名称
  • 监听连接和断开
  • 添加 多个Service,Characteristic,Descriptor
  • 支持read,write,notify
  • 支持长数据读取写入

待实现

  • 扫描别的蓝牙设备
  • 连接别的蓝牙设备
  • indicate ,write no response 的兼容

依赖

#ubuntu/debian

sudo apt install -y dbus-java-bin bluez bluetooth libbluetooth-dev libudev-dev

使用

初始化

BusConnector.init();

获取DBUS连接

BusConnector connector = BusConnector.getInstance();

获取蓝牙适配器

List<String> adapters = connector.findAdapters();

//这个列表是dbuspath的列表,使用connector.makeModel(),生成适配器示例

BleAdapter adapter = connector.makeModel(BleAdapter.class,adapters.get(0));

创建服务和特征

new AdvertiseBuilder(adapter,"/bttest")
                .addService(new ServiceBuilder(SERVICE_UUID1)
                        .setPrimary(true)
                        .addCharacteristic(new CharacteristicBuilder(CHARACTERISTIC_UUID1)
                                .setReadDataHandler((characteristic, options) -> "hello".getBytes())
                                .setWriteDataHandler((characteristic, value, options) -> {
                                    System.out.println("Write:" + Hexdump.toHex(value));
                                })
                                .setNotifyHandler(notify -> {
                                    System.out.println("NotifyChange:" + notify);
                                })
                        )
                        .addCharacteristic(new CharacteristicBuilder(CHARACTERISTIC_UUID2)
                                .setReadDataHandler((characteristic, options) -> "aa".getBytes())
                        )

                )

                .addService(new ServiceBuilder(SERVICE_UUID2)
                        .addCharacteristic(new CharacteristicBuilder(CHARACTERISTIC_UUID2)
                                .setReadDataHandler((characteristic, options) -> "aa".getBytes())
                        )
                        .setPrimary(true)
                )
                .setConnectionListener(new ConnectionListener() {
                    @Override
                    public void onDeviceConnected(Device1 dev, Map<String, Variant<?>> options) {
                        System.out.println("onDeviceConnected");
                        BleDevice bleDevice = null;
                        try {
                            bleDevice = connector.makeModel(BleDevice.class, dev);


                        } catch (DBusException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onDeviceDisconnected(Device1 dev) {
                        System.out.println("onDeviceDisconnected");
                    }
                })
                .setAdvertiseType(AdvertiseType.BROADCAST)
                .setAdvertiseBleName("adddd")
                .build();

设置蓝牙信息

adapter.setPowered(true);
adapter.setDiscoverable(true);
adapter.setDiscoverableTimeout(0);

设置广播名称,类型,连接监听(也可以在Builder设置)

advertiser.setAdvertiseName();
advertiser.setAdvertiseType();
advertiser.setConnectionListener();

connector.findDevices()

广播开始和停止

//广播开始前可以先断开已经连接的设备。
advertiser.disconnectAllDevices();

advertiser.startAdvertise();

advertiser.stopAdvertise();

其他

//获取已经连接的设备 返回的是dbuspath,
//使用connector.makeModel(),生成连接设备实例
connector.findDevices();

//断开指定设备,使用这两个都行

bleDevice.disconnect();

bleAdapter.removeDevice();

bleadvertisement's People

Contributors

ve3344 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

bleadvertisement's Issues

这个在windows下面不能运行吗,初始化就报错了

Exception in thread "main" org.freedesktop.dbus.exceptions.DBusException: Cannot Resolve Session Bus Address
at org.freedesktop.dbus.connections.impl.DBusConnection.getDbusMachineId(DBusConnection.java:272)
at org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:122)
at org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:97)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.