Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

device disconnects immediately after connecting #766

Open
ClaeysJoke opened this issue May 20, 2020 · 4 comments
Open

device disconnects immediately after connecting #766

ClaeysJoke opened this issue May 20, 2020 · 4 comments

Comments

@ClaeysJoke
Copy link

Hello! I am trying to connect with a temperature measuring device from Andesfit. However, after connecting with the device, it's immediately disconnected. Can any of you help? Thank you!

var app = {
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },
    onDeviceReady: function() {
        this.receivedEvent('deviceready');
        this.startScanForDevices();
        //ble.autoConnect("B4:52:A9:C5:9D:6E",app.onConnected,app.onDisconnected);
    },
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },

    onDiscoverDevice: function(device){
        var devicediscovered=0;
        console.log(JSON.stringify(device));
        var listItem = document.createElement('li');
        listItem.innerHTML = device.name + '<br/>' +
            device.id + '<br/>' +
            'RSSI: ' + device.rssi;
        deviceList.appendChild(listItem);

        if(device.name=='TEMP'){
            console.log('temp found');
            console.log(device.id);
            ble.connect(device.id,app.onConnected,app.onDisconnected);
        }
    },

    onDisconnected:function(device){
        console.log('disconnected temp device');
    },

    onDisconnectedState:function(device){
        console.log('disconnected temp device state');
    },

    onConnected: function(device){
        console.log('connected with temp device');
        ble.isConnected(device.id, app.onConnected, app.onDisconnectedState);
        console.log(JSON.stringify(device));
        console.log('trying to start notification')
         ble.startNotification(device.id, "1809", "2A1C", app.onNotificationStarted, function() {
             console.log('start notification failed');
        });
    },

    onNotificationStarted: function(buffer){
        console.log('notification started');
        var data = new Uint8Array(buffer);
        console.log("Temp state changed to " + data[0]);
    },

    onNotification2Started: function(buffer){
        var data = new Uint8Array(buffer);
        console.log("Temp state 2 changed to " + data[0]);
    },

    startScanForDevices: function() { 
        console.log('scanning started');
        ble.startScan([], app.onDiscoverDevice, function() {
            console.log('scanning failed');
        });
        setTimeout(ble.stopScan,
            5000,
            function() { console.log("Scan complete"); },
            function() { console.log("stopScan failed"); }
        );
     } 
};

app.initialize();
@ClaeysJoke
Copy link
Author

Update: if I remove "ble.startnotification", I get the feedback that it is disconnected from "onDisconnectedState" but not from "onDisconnected"

@don
Copy link
Owner

don commented Jun 22, 2020

The device probably expects an initialization message after you connect. If there's no documentation, try using the manufacturers app and sniffing the bluetooth communication to see what it's doing.

@benplain
Copy link

@ClaeysJoke,

I found that with my devices (heart rate monitors), when I used ble.connect it dropped the connection instantly. However, when I use ble.autoConnect, it keeps the connection open and I don't get the instant disconnect.

I'm not sure if this is the correct way to do it but it works for me.

@Gargamil
Copy link
Contributor

@ClaeysJoke,

I found that with my devices (heart rate monitors), when I used ble.connect it dropped the connection instantly. However, when I use ble.autoConnect, it keeps the connection open and I don't get the instant disconnect.

I'm not sure if this is the correct way to do it but it works for me.

I have exactly the same problem, but only on Android. iOS works with "normal" connect perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants