Skip to content

hoseinnikmaram/Android-PeerMessanger

 
 

Repository files navigation

Android-PeerMessanger
Step 1:We need the Android Bluetooth service for this tutorial to work. In order to use Bluetooth service, declare BLUETOOTH permission in manifest file.
Step 2:Now to check whether Bluetooth is supported on device or not, we use object of BluetoothAdapter class.
Step 3:If Bluetooth is disabled then we request the user to enable it. And we perform this action by calling startActivityForResult() with REQUEST_ENABLE_BT action.
This will open dialog to enable Bluetooth on the device.
Step 4:Now in android, device is not discoverable by default. To make device discoverable, call startActivityForResult() with BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE action.
Step5:To start the chat, we first need to establish connection with the desired device. And before starting scanning for available devices, we usually get paired devices first in the list
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
Set pairedDevices = bluetoothAdapter.getBondedDevices()
Step6:To connect two devices, we must implement server side and client side mechanism. One device shall open the server socket and another should initiate the connection. Both are connected when BluetoothSocket is connected on the same RFCOMM channel.
During connection procedure android framework automatically shows pairing dialog.

alt text
Step 7: Read and Write Data
After establishing connection successfully, each device has connected BluetoothSocket.
Now one can Read and write data to the streams using read(byte[]) and write(byte[]).
alt text

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published