Discovering andc controlling an already paired BLE device

I’m tasked with writing a configuration tool for a BLE HID device. The tool will ideally run on iOS/Android/macOS/Win/ChromOS so our initial thought was a web app using the Web Bluetooth API. There is a fundamental problem we can’t figure out how to work around.

If our device is paired with the OS and our application is launched it needs to discover the device and then control it using a custom service. Calling requestDevices() lets us discover and control UNPAIRED devices that are advertising, but doesn’t enumerate the connected devices. getDevices() enumerates the connected devices but subsequent calls to access services fail.

So, Is what we want to do possible with Web Bluetooth or any other cross-platform approach(electron, flutter, etc)?

// Connect to the device
try {
console.log('Getting existing permitted Bluetooth devices...');
const connectedDevices = await navigator.bluetooth.getDevices();
console.log('Got ' + connectedDevices.length + ' Bluetooth devices.');
for (const possibleDevice of connectedDevices) {
    try {
        const server = possibleDevice.gatt;
        console.log('Device: ' + possibleDevice.name + ' (' + possibleDevice.id + ') - Connected: ' + server.connected);
        if (!server.connected) {
            try {
                await server.connect();
            }
            catch (error) {
                console.error('Error during reconnection process:', error);
            }
        }

        console.log('Getting Device Information Service...');
        const service = await server.getPrimaryService('device_information');
        const model = await service.getCharacteristic(model_number_string);
        await model.readValue().then(value => {
            console.log('Model: ' + decoder.decode(value));
            if ((decoder.decode(value)).includes('Cato')) {
                console.log('Found Cato');
                setDevice(possibleDevice);
            }
        })
        if (possibleDevice === device)
            break

    } catch (error) {
        console.error('Error during connection process:', error);
    }
}

Connect to unpaired device works:

BluetoothConnector.js:66 Looking for devices...
20:37:00.163 BluetoothConnector.js:72 Device selected: BluetoothDevice {
      id: 'z+Z+osCNpR6WsoaF3iJStQ==', name: "spero's cato (1)",
      gatt: BluetoothRemoteGATTServer, ...
20:37:01.222 BluetoothConnector.js:75 Connected to GATT server.
20:37:01.756 BluetoothConnector.js:78 Motion service obtained: 
20:37:01.756 BluetoothConnector.js:81 Enable characteristic obtained: 

Subsequently trying to reconnect after browser refresh fails

Navigated to http://localhost:3000/dashboard
20:37:28.145 BluetoothConnector.js:22 Getting existing permitted Bluetooth .
20:37:28.147 BluetoothConnector.js:24 Got 4 Bluetooth devices.

BluetoothConnector.js:28 Device: spero's cato (1) (z+Z+osCNpR6WsoaF3iJStQ==) - Connected: false
BluetoothConnector.js:34 Error during reconnection process: NetworkError: Bluetooth Device is no longer in range.
BluetoothConnector.js:38 Getting Device Information Service...
20:37:28.152 BluetoothConnector.js:52 Error during connection process: NetworkError: Failed to execute 'getPrimaryService' on 'BluetoothRemoteGATTServer': GATT Server is disconnected. Cannot retrieve services. (Re)connect first with device.gatt.connect`.

New contributor

sperok is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật