简体   繁体   中英

How to send message between two Android devices via bluetooth or BLE?

Is it possible to send messages bi-directionally between two Android devices via bluetooth or bluetooth low energy? You can assume that each Android device has the same Android app running. If so, could you please share how this might be possible? Thanks!

Yes it can be possible, but is not so easy. First you need to read documentation

Android bluetooth

and later try find some example which will resemble your needs.

Example : bluetooth chat

Comment : I did communication android with special hardware (I think FTDI Chip), but with Xamarin .NET. Some things will be the same. Mostly it is done so that the UI, creating communication and data transfer are in a separate Thread. In reading thread you have endless cycle in which the reading bytes from the input stream and save it into temp buffer (impeding receipt and you will not lose bytes). Later moves to MainBufferu and work with them.

UPDATE

You have to read the documentation and explore example. I can not describe everything here. I can roughly describe the lifecycle of how I dealt with itself: When you have paired the device ( you get BluetoothDevice ) start ConnectThread which opens RfcommSocket and try Connect on it. If it is OK, than start ConnectedThread where you have endless cycle of reading data from InputStream. In cycle You put your data into a temp buffer and once in a while moved data into MainBuffer where do you take them for processing. If you want send data via Bluetooth, you will just write to the OutputStream in ConnectedThread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM