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

Application is not working #1

Open
ShaileshBhatNP opened this issue Jun 8, 2020 · 16 comments
Open

Application is not working #1

ShaileshBhatNP opened this issue Jun 8, 2020 · 16 comments

Comments

@ShaileshBhatNP
Copy link

Hi,
I ran this application in VS2019. I didn't get any error while building, running..To check in real time, I have created apk file. But it is not working(not showing error..Other than State button, all other button actions not responding with results as expected) ..while checking with USB debugging also, sometimes getting exceptions..

@msthrax
Copy link
Owner

msthrax commented Jun 8, 2020

hello,
thank you for using this library.
Could you tell me about your situation and give me more information.(for example your platform).

I may notice about these points:

  1. You should turn on Bluetooth your self. The library itself is not capable of that.
  2. This lib was only implemented on Android platform.
  3. Try debugging the app on your phone and check the code with some breakpoints. Avoid running app without debugger.

Thank you :)

@ShaileshBhatNP
Copy link
Author

ShaileshBhatNP commented Jun 9, 2020

  1. Turned on the bluetooth
  2. My device is also Android
  3. Debugged the app on my phone and checked the code with some breakpoints.

Android version of my phone- 8.1.0,

My problem is,
deviceList.Add(a.Device); this line is not adding nearer bluetooth devices, even though all my devices are turned on with bluetooth and location also..

@msthrax
Copy link
Owner

msthrax commented Jun 9, 2020

Hi
Please check that, you can get list of Paired devices?
Paired devices will not be added to device list. You can check than in "Button_Start_Clicked";
listView_DeviceList.ItemsSource = CrossBluetooth.Adaptor.GetListOfDiscoveredDevices(); listView_PairedDeviceList.ItemsSource = CrossBluetooth.Adaptor.GetPairedDevices();
Also please use debugger to check the DefaultBluetoothAdaptor has value or not?

@ShaileshBhatNP
Copy link
Author

listView_PairedDeviceList.ItemsSource = CrossBluetooth.Adaptor.GetPairedDevices();
Above is getting proper list. But below line
listView_DeviceList.ItemsSource = CrossBluetooth.Adaptor.GetListOfDiscoveredDevices();
is not giving, even though delay time is increased to 30000

@msthrax
Copy link
Owner

msthrax commented Jun 12, 2020

I did not get my answer.
If when a device is paired to your phone, it will not be shown in discovered list.

@ShaileshBhatNP
Copy link
Author

ShaileshBhatNP commented Jun 12, 2020

If it is not paired, I should get it in ListOfDiscoveredDevices() right?(I have one paired and one unpaired device)..
To get ListOfDiscoveredDevices(), what I have to do..and
when will below code executes?..(Connect button Action)
if (CrossBluetooth.Adaptor.ConnectToDeviceByName(Entry_BluetoothDeviceName.Text)) { bluetoothWriter = new BinaryWriter(CrossBluetooth.Adaptor.GetTransmitStream()); bluetoothReader = new CustomStreamReader(CrossBluetooth.Adaptor.GetReceiveStream()); backgroundWorker.RunWorkerAsync(); }

@msthrax
Copy link
Owner

msthrax commented Jun 12, 2020

[Yes, if not paired you should get it on discovered device.
At this moment I really dont know what is happening. Every thing was right when I tested the app again.
Did you debug and check the Android implementation(MainActivity.cs)?

@ShaileshBhatNP
Copy link
Author

ShaileshBhatNP commented Jun 12, 2020

After checking the Android implementation, I found that in below code,

            public List<CrossBluetoothDevice> GetListOfDiscoveredDevices()
            {
                List<CrossBluetoothDevice> res = new List<CrossBluetoothDevice>();
                foreach (var item in deviceList)
                {
                    res.Add(new CrossBluetoothDevice(item.Name,item.Address));
                }
                return res;
            }
            public List<CrossBluetoothDevice> GetPairedDevices()
            {
                List<CrossBluetoothDevice> res = new List<CrossBluetoothDevice>();
                foreach (var item in bluetoothAdapter.BondedDevices)
                {
                    res.Add(new CrossBluetoothDevice(item.Name, item.Address));
                }
                return res;
            } 

deviceList is not getting added..but there will be bluetoothAdapter.BondedDevices(from which we can get paired devices)

Further checking, below code is not executing..

            protected virtual void OnDeviceDiscovered(CrossBluetoothDevice BluetoothDevice)
            {
                if (DeviceDiscovered != null) DeviceDiscovered(this, new DeviceDiscoveredEventArgs(BluetoothDevice));
            }
            public void AddNewDevice(BluetoothDevice bluetoothDevice)
            {
                deviceList.Add(bluetoothDevice);
                OnDeviceDiscovered(new CrossBluetoothDevice(bluetoothDevice.Name, bluetoothDevice.Address));
            }

when these should run? Can you please help..

@ShaileshBhatNP
Copy link
Author

ShaileshBhatNP commented Jun 13, 2020

After enabling the Location Permission for the app(earlier, I turned on Device Location, but we should allow the app to get device location), all my above problems are solved out. Thank you..

But new exception rised while clicking Connect button

Java.IO.IOException
Message=read failed, socket might closed or timeout, read ret: -1

at below line

bluetoothTxSocket = bluetoothDevice.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
bluetoothTxSocket.Connect();

@msthrax
Copy link
Owner

msthrax commented Jun 16, 2020

Hi.
I'm glad that your problem is solved.

The reason of that exception is: The bluetooth connection is working in Serial Port Profile.
So you need to open a serial port on destination device. As soon as you open the corresponding serial port on destionation device, connection will be established. If you arer using PC(Windows) as destionation device, you can easily open related serial port. But if you are using a phone as destionation device you should handle connecttion request your self.

@ShaileshBhatNP
Copy link
Author

After replacing commented code with Task.Run, I'm not getting above exception now.

//bluetoothTxSocket.Connect();
Task.Run(() => { bluetoothTxSocket.Connect(); });

But, in below set of code, after executing streamReader = bluetoothRxSocket.InputStream;, I'm getting System.NullReferenceException
Message=Object reference not set to an instance of an object.
, since bluetoothRxSocket is null.

bluetoothServerSocket = bluetoothAdapter.ListenUsingRfcommWithServiceRecord(context.PackageName, UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
//bluetoothRxSocket = bluetoothServerSocket.AcceptAsync();
Task.Run(() => { bluetoothRxSocket = bluetoothServerSocket.Accept(); });
streamReader = bluetoothRxSocket.InputStream;

Sorry, I didn't get how to open the serial port on destination device, whether it may be PC/Phone..

@msthrax
Copy link
Owner

msthrax commented Jun 24, 2020

Hi, I'm sorry that I couldn't comment on the problem. I need some time to determine what is happening with your problem.

@ShaileshBhatNP
Copy link
Author

Hi, whether your sample is for connecting to Classic bluetooth devices / BLE devices?..

@msthrax
Copy link
Owner

msthrax commented Jun 26, 2020

It is for classic bluetooth devices.

@ShaileshBhatNP
Copy link
Author

Do you have any alternatives for connecting to BLE devices..?

@msthrax
Copy link
Owner

msthrax commented Jul 7, 2020

As far as I know, you should consider Bluetooth profiles. Each BLE device works in one or multiple profiles. Take a look at Google sources (Java) about the Bluetooth. You will get good answers from it. Also, you can check Bluetooth Standards implementations in Java which can be found in Xamarin.Android.

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

2 participants