bcsphere / bluetooth

Please support! Kindly "Star" this standard Bluetooth Smart API / SDK :) The "bcsphere/bluetooth" is a phonegap/cordova plugin. It's a Bluetooth JavaScript API from JUMA, a Bluetooth expert team. It supports both Bluetooth 4.0 GATT/Bluetooth Low Energy (BLE) interface in iOS/Android and Bluetooth 2.1 Classic Rfcomm / L2cap socket interface as Bluetooth Serial Port/SPP, as well as iBeacon!
Apache License 2.0
298 stars 12 forks source link

GetConnectedDevices empty when both devices are in ConnectedThread #34

Open sliwa886 opened 9 years ago

sliwa886 commented 9 years ago

Hi There,

I'm trying to create an Android app where two devices can send to each other message. Everything works fine (Im using bluetooth2.1 rfcomm connection) but i have small problem in java script. When connection is created beetwen two devices (on java side they are in ConnectedThread) In java script method GetConnectedDevices() is returning empty table on both devices.

And another question: Is there easy way to create addEventListener to read ANY message which is coming to my device. From documantation i understand that u must have device object and then run method rfcommRead . But I dont know how to get this object for now.

Sorry For My Bad English :(

Here is my code:

if(!BC.bluetooth.isopen){ BC.Bluetooth.OpenBluetooth(init,function(){alert("There is problem with bluetooth. Please restart application");}); }else{ init(); }

function init(){ $scope.blueReady = true; BC.Bluetooth.RFCOMMListen("appName","7A9C3B55-78D0-44A7-A94E-A93E3FE118CE",false);

        };

        $scope.startScan = function(){
            BC.Bluetooth.StartScan("Classical");
            alert("Started Scan");

        };

        $scope.connectDevice = function(device){
            BC.Bluetooth.StopScan();
            alert("connection start: "+ JSON.stringify(BC.bluetooth.devices[device.deviceAddress]));
            device = BC.bluetooth.devices[device.deviceAddress];
            device.connect(function(){writeMSG(device)},function(){alert("error!");},"7A9C3B55-78D0-44A7-A94E-A93E3FE118CE",false);

        };

        var writeMSG = function(device){
            alert("device is already connected well! is connected: "+device.isConnected);
            device.rfcommWrite("Hex","01",writeSuccess);
            function writeSuccess(data){
                alert("write success!" + JSON.stringify(data));
            }
        };

        var readMSG = function(device){

                device.rfcommRead(readSuccess);

            function readSuccess(data){
                alert("Data : "+JSON.stringify(data.value)+" \n Time :"+data.date);
            }
        };

        BC.bluetooth.addEventListener("newdevice",addNewDevice);
                function addNewDevice(s){
                    var newDevice = s.target;
                    $scope.devices.push(newDevice);
                    readMSG(device);
                    alert("new device be found! it's device Address is: " + JSON.stringify(newDevice));
                };

        $scope.connectedDevices = function(){
            BC.Bluetooth.GetConnectedDevices(function(mes){alert("this is connected: "+JSON.stringify(mes));});
        };