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

GetConnectedDevices empty when both devices are in ConnectedThread #34

Open
sliwa886 opened this issue Jan 18, 2015 · 0 comments
Open

Comments

@sliwa886
Copy link

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));});
        };
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

1 participant