Using Xamarin to create android bluetooth terminal to connect to HC-06 module

I am trying to create a very simple application that would allow me to communicate with an HC-06 chip (simple UART interface) from Android very similar to BlueTerm or something similar currently on the play store. The standard bluetooth example provided by Xamarin will work fine but doesn't connect to the bluetooth module (just says it can't connect to the device). If anyone has any ideas why this might be done and / or how to fix it, it would be much appreciated.

+3


source to share


2 answers


"HC05" operates in both "master" and "slave" modes. "Hc05" is the default for slave mode, eg hc06.

I found on blog 'xamarin' this link 3 years ago: https://blog.xamarin.com/hello-mr-bond_agent_watch/

This page is about some kind of smartwatch that is built with a microcontroller, but you can download an Android project called "NetduinoBT_AgentProto" which completely contains everything you need to connect to Bluetooth SPP (Serial Port Profile).

add the App.cs class to your project.

and these permissions:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

      



To get a list and name of devices:

var list = App.Current.BondedBluetoothDeviceNames; var name = App.Current.BondedBluetoothDeviceNames[yourIndex];

To connect to a device: bool connected = App.Current.ConnectToBluetoothDevice(name , out message);

You can set the MessageReceived event to receive a response to the device: string mYourResult; ... protected override void OnCreate (Bundle bundle) { ... App.Current.MessageReceived += (object sender, App.MessageReceivedEventArgs e) => { RunOnUiThread( () => { mYourResult = e.MessageString; }); }; ... }

To send a string: App.Current.SendStringToBluetoothDevice("Your String");

in my experience, on some Linux kernel based systems, you need to add the following line to the end of your line. Some things like: "\ r \ n" or "\ n" or "\ r" or Environment.NewLine

+1


source


This is the app I used to test the HC-05 and HC-06 modules.
BluetoothTerminal
It's very simple. Just connect to the device and send data. The resulting data will also be shown.



0


source







All Articles