Bug 27379 - [DEVICE SPECIFIC] - Bluetooth Stream Read / IsDataAvailable Failing
Summary: [DEVICE SPECIFIC] - Bluetooth Stream Read / IsDataAvailable Failing
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.20.0
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-02-24 17:09 UTC by Jon Douglas [MSFT]
Modified: 2015-08-11 10:44 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Not Working Log (11.82 KB, text/plain)
2015-02-24 17:12 UTC, Jon Douglas [MSFT]
Details
Working Log (25.77 KB, text/plain)
2015-02-24 17:13 UTC, Jon Douglas [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Jon Douglas [MSFT] 2015-02-24 17:09:40 UTC
*Description

Debugging shows that
the stream.IsDataAvailable function does not return. It does not return
anything and execution stops. A code snippet:

Log.Info("BT", "before wait for read clear");
while (mmInStream.IsDataAvailable())
xbytes = mmInStream.Read(buffer, 0, buffer.Length);
Log.Info("BT", "after wait for read clear");

The log never shows "after wait for read clear" and a breakpoint set at
the .Read statement is never hit.

For testing, I commented out the above code. In another spot in the App
I have this statement:
if(mmInStream.IsDataAvailable())

It does the same thing above if there is no data. But, if there is data
available on the input stream, it will return and BT communications work
fine (of course until there is no data in the stream and it locks up as
before).

*Replication
Using a {Doogee Titans DG150} device and running the sample attached, you will notice that there is no data available when connecting to BT. However using other devices(Nexus 6, Samsung Galaxy s4, etc), this seems to work just fine.

2 buttons: "Select Bluetooth device" and "Send message"

The failing device (Doogee) will get stuck at IsDataAvailable statement
as shown in the comments. A working Android device will not get stuck
and will show a status display of "Rx: 0".

*Device Model
Doogee Titans DG150
Android 4.2.2

*Attempted Workarounds

Method m = _device.Class.GetMethod ("createRfcommSocket", new Java.Lang.Class[] {Java.Lang.Integer.Type});
socket = (BluetoothSocket) m.Invoke(_device, 1);

http://stackoverflow.com/questions/3397071/service-discovery-failed-exception-using-bluetooth-on-android

http://stackoverflow.com/questions/3031796/disconnect-a-bluetooth-socket-in-android/3274819#3274819

*Additional Logs

I have additionally attached a couple logcat files displaying BT working on a normal device, and BT not working on the {Doogee Titans DG150} device

*Version Information

=== Xamarin Studio ===

Version 5.7.1 (build 17)
Installation UUID: 205770f1-e611-41a3-925c-7560f7e24eb1
Runtime:
	Mono 3.12.0 ((detached/de2f33f)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000076

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/jondouglas/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.6.1.26 (Business Edition)
Hash: 98ee412
Branch: 
Build date: 2015-02-11 04:37:05-0500

=== Xamarin.Mac ===

Not Installed

=== Build Information ===

Release ID: 507010017
Git revision: 0bc7d3550b6b088ac25b08dcf7bbe73bcc8658b3
Build date: 2015-02-03 19:43:29-05
Xamarin addins: f7b7d34419c9ec24501bfa7c658e80a6305613e0

=== Operating System ===

Mac OS X 10.10.0
Darwin Jons-MacBook-Pro.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 1 Jon Douglas [MSFT] 2015-02-24 17:12:58 UTC
Created attachment 10013 [details]
Not Working Log
Comment 2 Jon Douglas [MSFT] 2015-02-24 17:13:13 UTC
Created attachment 10014 [details]
Working Log
Comment 4 Jonathan Pryor 2015-02-24 17:25:28 UTC
> while (mmInStream.IsDataAvailable())

If mmInStream is a java.lang.InputStream, then the IsDataAvailable() extension method is equivalent to calling InputStream.available() and seeing if the value is greater than 0:

http://developer.android.com/reference/java/io/InputStream.html#available()

If `mmInStream.IsDataAvailable()` is itself blocking, this in turn suggests that InputStream.available() is blocking, which sounds like an Android issue, not a Xamarin issue.

It would be interesting to know what mmInStream.Class.Name is...but not necessary.

Furthermore, the fact that Java apps hit the same issue -- as evidenced by the stackoverflow URLs in Comment #0 -- further suggests that this isn't a Xamarin bug.
Comment 5 Jeffrey Dobos 2015-03-03 11:30:52 UTC
Not Resolved or answered. A program written in Java called Bluetooth Chat works on the Doogee device.  It works even when no data is available.

Another point: The same issue occurs when attempting to read the input stream with no data present using inputstream.read() on the Doogee device.  Comment 4 does not address this issue.
Comment 6 Jonathan Pryor 2015-03-03 12:21:00 UTC
> A program written in Java called Bluetooth Chat works on the Doogee device

What's that program *do*? Do you have source to it? Does it call InputStream.available() on a Bluetooth-backed stream? Is it actually comparable/similar code?

If it's the Android SDK sample, then the answer is *No*, it doesn't call InputStream.avaialble(); it calls InputStream.read() within a try/catch block:

https://github.com/android/platform_development/blob/2af724c6eb74259bb8118398f1a7ff39955be297/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java#L454

I have no idea what the behavior of InputStream.available() is, but (based in the C# behavior reported in Comment #0) it would not surprise me that it blocks forever.

For that matter, look at the Java code! mmInStream.read() is within a run() method on a type that extends Thread! It's running on a dedicated thread! mmInStream.read() BLOCKS FOREVER; see the `catch` block, which reports *any* exception as "disconnected".

I'm not seeing any evidence that this is a Xamarin.Android bug, and that behavior in any way differs from what would happen with Java.
Comment 7 Ben Foote 2015-08-11 10:44:55 UTC
I'm running into this exact same problem. Both .Read and .IsDataAvailable both eventually block.

This doesn't happen in a Java android app. The Bluetooth Chat example mentioned in Comment 5 works as expected using a simple try/catch block around a call to .Read.

See here: https://android.googlesource.com/platform/development/+/eclair-passion-release/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java

This is a deal breaker for me and my company. We're going to switch back to using Java for our Android apps.

Not resolved, or answered.