Not able to connect with bluetooth device with socket in android studio - android

Hello everyone I am new to android development i am connecting with bluetooth device as a client in different class thread. If Nullpointerexcception occur then i use default UUID. After this when i use socket.connect() it show debug warning and don't send any pair request to device. Nothing happen. I am new to android development if any one can help. Thank you in advance.
Warning and Logs
here is my code in Thread;
class ConnectWithDevice(context : ConnectWithBluetooth, device : BluetoothDevice) : Thread(){
private val mContext : ConnectWithBluetooth = context
private val mmSocket : BluetoothSocket
private val mmDevice : BluetoothDevice
// Default UUID
private val mmDefaultUUID = UUID.fromString("78c374fd-f84d-4a9e-aa5b-9b0b6292952e")
init {
var temp : BluetoothSocket? = null
mmDevice = device
try {
temp = device.createRfcommSocketToServiceRecord(mmDevice.uuids[0].uuid)
}catch (en : NullPointerException){
en.printStackTrace()
temp = device.createRfcommSocketToServiceRecord(mmDefaultUUID)
}catch (e : IOException){
e.printStackTrace()
Log.e("TAG","Socket's create() method failed",e)
}
mmSocket = temp!!
Log.i("TAG","Got the Socket")
}
override fun run() {
// Cancel discovery because it otherwise slows down the connection.
if(mContext.bluetoothAdapter != null){
mContext.bluetoothAdapter!!.cancelDiscovery()
}
try{
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
Log.i("TAG","Connecting...")
mmSocket.connect()
Log.i("TAG","Bluetooth Successfully Connected")
}catch (connectException : IOException){
// Unable to connect; close the socket and return.
try{
mmSocket.close()
}catch (closeException : IOException){
Log.e("TAG","Could not close the client socket",closeException)
}
return
}
// The connection attempt succeeded. Perform work associated with
// the connection in a separate thread.
Log.i("TAG","Device is Connected")
//manageMyConnectedSocket(mmSocket)
}
// Closes the client socket and causes the thread to finish.
// Call this method from the main activity to shut down the connection.
fun cancel(){
try {
mmSocket.close()
} catch (e: IOException) {
Log.e(ContentValues.TAG, "Could not close the client socket", e)
}
}
}

Finally i find the solution here it is ->
I use this code ->
val m = device.javaClass.getMethod("createRfcommSocket", *arrayOf<Class<*>>(Int::class.java))
temp = m.invoke(device, 1) as BluetoothSocket
Instead of this ->
temp = device.createRfcommSocketToServiceRecord(mmDevice.uuids[0].uuid)

Related

Cannot connect to socket.io in android , saying invalid namespace

When i try to connect my android app with socket.io, it saying invalid namespace. There is no issue with socket url , but having problem in connection, please help if you faced this issue ever. Thanks!
val options = io.socket.client.IO.Options().apply {
forceNew = false
reconnection = true
}
private val socket: Socket = io.socket.client.IO.socket(socket_url,options)
val socket = applicationAccessorImpl.socket
socket.connect()
socket.once(Socket.EVENT_CONNECT) {
try {
Log.d(TAG, "connect")
} catch (e: Exception) {
Log.d(TAG, e.message!!)
}
}.on(Socket.EVENT_CONNECT_ERROR) {
val e = it[0]
Log.e(TAG, "error $e")
}.on(Socket.EVENT_DISCONNECT) {
val e = it[0]
Log.e(TAG, "Transport error $e")
}
Sorry , it was my mistake , i was passing the wrong url 😋

Android Kotlin - How can I programmatically disconnect a bluetooth device?

Right now I have a code that connects the phone to a bluetooth printer, the problem is that I want to close that connection while not needing it, in other words I just want the device to connect to the printer when it needs to send a file and I want it to then disconnect.
To connect to the printer I'm using the following code:
fun connectBluetooth(address: String): Boolean {
var connectSuccess = true
try {
if (bluetoothSocket == null) {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
val device: BluetoothDevice = bluetoothAdapter!!.getRemoteDevice(address)
bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(myUUID)
bluetoothSocket!!.connect()
}
} catch (e: IOException) {
connectSuccess = false
e.printStackTrace()
}
return connectSuccess
}
I'm wondering how I can then close the connection, right now I have been simply doing
bluetoothSocket = null
but I highly doubt that is the right way to do it
Ok it wasn't as hard as I thought it would be, all that had to be done was to close the output stream that I was using in order to send the file to the printer.
I found out about this thanks to the link that was refered to me in the comments by vyas dipak: Disconnect a bluetooth socket in Android
All I did was create this small function and it all worked out well
fun disconnectBluetooth(){
val outputStream : OutputStream? = null
try {
outputStream?.close()
} catch (e: IOException ){}
try {
bluetoothSocket?.close()
} catch (e: IOException){}
bluetoothSocket = null
}

Android - Initiate Bluetooth Socket - connection timeout

I need to connect to a bluetooth device which acts as a server. I know its UUID (at least the device's documentation contains it). However, I get an exception when I try to connect to it. The discovery part takes place successfully.
In the following, I cite the relevant code parts.
Here is the discovery. After I successfully found my device, I try to connect to it.
private val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
private val bluetoothReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action: String = intent.action
when (action) {
BluetoothDevice.ACTION_FOUND -> {
val foundDevice: BluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
Log.i("NAME", foundDevice.name)
if (foundDevice.name.startsWith("RN487")) {
bluetoothAdapter?.cancelDiscovery()
device = foundDevice
val connectThread = ConnectThread(device)
connectThread.start()
}
}
}
}
}
private lateinit var device: BluetoothDevice
The ConnectThread class is here:
private inner class ConnectThread(device: BluetoothDevice) : Thread() {
private val mSocket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
device.createRfcommSocketToServiceRecord(UUID)
}
override fun run() {
bluetoothAdapter?.cancelDiscovery()
mSocket?.use { socket ->
socket.connect()
toast("Connected!")
}
}
fun cancel() {
try {
mSocket?.close()
} catch (e: IOException) {
Log.e(TAG, "Could not close the client socket", e)
}
}
}
The UUID was given as
private val UUID = nameUUIDFromBytes("49535343-...".toByteArray())
Thanks for your time and expertise!
As one of my eagle-eyed colleagues pointed out, the bluetooth description begins with the "oldschool" version on the official android developers site. Later, the bluetooth low energy is described, which I need for my project.

message not received in Bluetooth chat. Is my handler broken?

I am working on the project on the android dev. the website for BluetoothChat and it seems to be sending messages correctly but not receiving them. In the past,
I attempted to create another Bluetooth app and when connecting to a device I was prompted to confirm the connection and presented with a pin code. when working on the current app I am never given a prompt. However, it is not hitting any exceptions and messages seem to be sending. Here is my Thread class for managing a connection:
inner class ConnectedThread(val socket:BluetoothSocket, val socketType:String) : Thread(){
val inStream = socket.inputStream
val outStream = socket.outputStream
init {
mState = STATE_CONNECTED
}
override fun run() {
Log.i(TAG, "BEGIN mConnectedThread")
val buffer = ByteArray(1024)
var bytes:Int
while (mState == STATE_CONNECTED){
try {
bytes = inStream.read(buffer)
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget()
}catch (ioe:IOException){
Log.e(TAG, "disconnected", ioe)
connectionLost()
break
}
}
}
fun write(buffer:ByteArray){
try {
outStream.writeMessage(buffer, mHandler)
}catch (ioe:IOException){
Log.e(TAG, "Exception during write", ioe)
}
}
fun cancel(){
try {
socket.close()
}catch (ioe:IOException){
Log.e(TAG, "close of connect socket failed", ioe)
}
}
}
}
and below i it the extension function used to write to the OutputStream:
fun OutputStream.writeMessage(buffer:ByteArray, handler:Handler? = null){
write(buffer)
handler?.let {
it.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget()
}
}
I just am not sure where my mistake is. I was thinking it was a problem with the way that I am sending the message? But when I step through my code with the debugger I really don't see anything out of place. Now I am thinking that the problem is at the end of the receiving device which doesn't seem to receive anything at all. I will post my Handler and broadcastReceiver as well:
val mHandler = object:Handler(){
override fun handleMessage(msg: Message?) {
when(msg!!.what) {
Constants.MESSAGE_STATE_CHANGE -> {
when(msg.arg1) {
BluetoothChatService.STATE_CONNECTED -> {
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName))
mConversationArrayAdapter!!.clear()
}
BluetoothChatService.STATE_CONNECTING -> {
setStatus(getString(R.string.title_connecting))
}
BluetoothChatService.STATE_LISTEN -> {
}
BluetoothChatService.STATE_NONE -> {
setStatus(getString(R.string.title_not_connected))
}
}
}
Constants.MESSAGE_WRITE -> {
val writeBuf = msg.obj as ByteArray
val writeMessage = String(writeBuf)
mConversationArrayAdapter!!.add("Me: $writeMessage")
}
Constants.MESSAGE_READ -> {
val readBuf = msg.obj as ByteArray
val readMessage = String(readBuf, 0, msg.arg1)
mConversationArrayAdapter!!.add("$mConnectedDeviceName: $readMessage")
}
Constants.MESSAGE_DEVICE_NAME -> {
mConnectedDeviceName = msg.data.getString(Constants.DEVICE_NAME)
if (activity!=null)Toast.makeText(activity, "Connected to $mConnectedDeviceName", Toast.LENGTH_SHORT).show()
}
Constants.MESSAGE_TOAST -> {
if (activity!=null)Toast.makeText(activity, msg.data.getString(Constants.TOAST), Toast.LENGTH_SHORT).show()
}
}
}
}
val mReceiver:BroadcastReceiver = object:BroadcastReceiver(){
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (BluetoothDevice.ACTION_FOUND.equals(action)){
val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
if (device.bondState!=BluetoothDevice.BOND_BONDED){
mNewDevicesArrayAdapter.add("${device.name} \n ${device.address}")
}
}else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
setProgressBarIndeterminateVisibility(false)
setTitle(R.string.select_device)
if (mNewDevicesArrayAdapter.count==0){
val noDevices = resources.getText(R.string.none_found).toString()
mNewDevicesArrayAdapter.add(noDevices)
}
}
}
}
Here is my AcceptThread as well:
inner class AcceptThread(val secure:Boolean) : Thread(){
var mmServerSocket:BluetoothServerSocket? = null
val mSocketType:String = if (secure) "Secure" else "Insecure"
init {
try {
mmServerSocket = if (secure){
mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, UUID_SECURE)
} else {
mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME_INSECURE, UUID_INSECURE)
}
}catch (ioe:IOException){
Log.e(TAG, "Socket Type: $mSocketType listen() failed", ioe)
}
mState = STATE_LISTEN
}
override fun run() {
Log.d(TAG, "Socket Type: $mSocketType BEGIN $this")
name = "AcceptThread $mSocketType"
var socket:BluetoothSocket? = null
while (mState!=STATE_CONNECTED){
try {
socket = mmServerSocket!!.accept()
}catch (ioe:IOException){
Log.e(TAG, "Socket Type: $mSocketType accept failed", ioe)
break
}
}
socket?.let {
synchronized(this#BluetoothChatService){
when(mState){
STATE_LISTEN -> {}
STATE_CONNECTING -> {
connected(socket!!, socket!!.remoteDevice, mSocketType)
}
STATE_NONE -> {}
STATE_CONNECTED -> {
try {
it.close()
}catch (ioe:IOException){
Log.e(TAG, "Could not close unwanted socket", ioe)
}
}
else->{throw BluetoothChatServiceException("Invalid Service State")}
}
}
}
Log.i(TAG, "End mAcceptThread, Socket Type: $mSocketType")
}
fun cancel(){
Log.d(TAG, "Socket Type: $mSocketType cancel $this")
try {
mmServerSocket!!.close()
}catch (ioe:IOException){
Log.e(TAG, "Socket Type: $mSocketType close() of server failed", ioe)
}
}
}
Please let me know if you're able to spot anything. I would really appreciate the help. Also, I should mention that the original code was in Java. I am writing mine in Kotlin.
I noticed that your Kotlin when blocks aren't replicating the function from the Java code.
Kotlin:
when (mState) {
STATE_LISTEN -> {}
STATE_CONNECTING -> {
connected(socket!!, socket!!.remoteDevice, mSocketType)
}
...
is not the Java code:
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice(), mSocketType);
break;
...
but is instead this:
switch (mState) {
case STATE_LISTEN:
break;
case STATE_CONNECTING:
// Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice(), mSocketType);
break;
...
Your Kotlin code should be:
when (mState) {
STATE_LISTEN, STATE_CONNECTING -> {
connected(socket!!, socket!!.remoteDevice, mSocketType)
}
...
In Java the switch branch without the break falls through to the next line of code until the program hits a break while in Kotlin the entry with arrow/braces define a branch. Just double check all of your when statements to make sure they are following the expected Java switch behavior.
Turns out the problem was my while loop. The while(mState!=STATE_CONNECTED loop should have enclosed the rest of the run() function body. So the solution was just moving the curly brace. Sometimes it's the little things. But I do appreciate the help with my when expressions.

Bluetooth client Socket get same exception everytime

I am writing an Android app mostly in Kotlin that is supposed to scan for Bluetooth devices and also pair with them. I also want it to have a Bluetooth server socket running in the background to await connection attempts. However, I keep running into the same exception when attempting to invoke the BluetoothSocket.connect() method. The exception is:
10-10 20:07:57.917 18643-27894/com.example.zemcd.toofxchange E/Pairing Thread: error connecting
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:754)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:766)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:388)
at com.example.zemcd.toofxchange.PairingThread.run(BluetoothUtils.kt:83)
I read that this could be fixed with code similar to
btSocket = device.javaClass.getMethod("createRFcommSocket", Int::class).invoke(device, 1) as BluetoothSocket
But this does not work. It causes the app to crash with a ReflectException caused by NoSuchMethod. Also I have read that this is not a published method for a reason and I would like to try to use the published createRFcommSocketToServiceRecord() method. I am unsure of where to go from here, or what exactly is causing the IOException. Also, I never even get to the pairing screen. I am trying to find what is the cause of this exception, and how to fix it. My code:
class BluetoothUtils {
companion object {
val _UUID = UUID.fromString("a0e7e4c7-0e4e-43b7-9d18-659192512164")
val TAG = "BluetoothUtils"
fun initPairingServer(adapter: BluetoothAdapter){
var mmServerSocket: BluetoothServerSocket? = null
try {
var tmp = adapter.listenUsingRfcommWithServiceRecord(TAG, _UUID)
mmServerSocket = tmp
ListenThread(mmServerSocket).start()
}catch (ioe: IOException){
Log.e(TAG, "Error initializing Bluetooth", ioe)
}
}
fun pair(adapter: BluetoothAdapter, device: BluetoothDevice){
var btSocket: BluetoothSocket? = null
try {
adapter.cancelDiscovery()
btSocket = device.createRfcommSocketToServiceRecord(_UUID)
PairingThread(btSocket).start()
}catch (ioe: IOException){
Log.e(TAG, "error connecting", ioe)
}
}
}
}
class ListenThread(val btServSock: BluetoothServerSocket) : Thread(){
companion object {
val TAG = "ListenThread"
}
var btSocket: BluetoothSocket? = null
override fun run() {
super.run()
while (true){
try {
Log.d(TAG, "listening . . . ")
btSocket = btServSock.accept()
}catch (ioe: IOException){
Log.e(TAG, "Error", ioe)
break
}
//manage connection here
//with either BluetoothUtils function
//or BluetoothSocket extension
}
}
}
class PairingThread(val btSocket: BluetoothSocket) : Thread(){
companion object {
val TAG = "Pairing Thread"
}
override fun run() {
super.run()
try {
Log.d(TAG, "attempting to connect")
btSocket.connect()
}catch (ioe: IOException){
Log.e(TAG, "error connecting", ioe)
btSocket.close()
}
}
}
Please somebody help me find my problem. Could it be that I'm attempting to connect to a device that isn't using the same UUID? I am just trying to connect to my laptop.

Categories

Resources