Android studio читать bluetooth

Reading data from bluetooth device in android

I am using bluetooth chat in order to connect and recieve data from a bluetooth device. I use the following code for reading data:

public void run() < byte[] buffer = new byte[1024]; int bytes; Log.v("MR", "start listening. "); // Keep listening to the InputStream while connected while (true) < try < // Read from the InputStream Log.d("MR", "buffer in try"); bytes = mmInStream.read(buffer); Log.d("MR", "input stream :"+(new String(buffer))); // Send the obtained bytes to the UI Activity mHandler.obtainMessage(Conn.MESSAGE_READ, bytes, -1, buffer).sendToTarget(); Log.d("MR", "buffer after"); >catch (Exception e) < Log.e("MR", "Error :"+e.getMessage()); // connectionLost(); // break; >Log.d("MR", "buffer after while"); > > 
bytes=mmInStream.read(buffer); 

and never returns from that call. I guess this is because it starts reading data from the device and doesn’t stop until it disconnects. How can I read a certain amount of bytes at a time? EDIT Unless it stay to the bytes = mmInStream.read(buffer); code due to that it don;t get any data back on from the device?

th problem might not be the certain that i havce write on the top. i think it must be the port that it’s make the communication. is it possible for the two devices to be connected but on the socket nothing to be transmitted os there is something that going wrong with the communication?

1 Answer 1

I use DataInputStreams instead as you can do a readFully() method which waits to read a certain number of bytes before returning. I setup the BT connection like this:

BluetoothDevice btDevice = bta.getRemoteDevice(macAddress); BluetoothSocket btSocket = InsecureBluetooth.createRfcommSocketToServiceRecord( btDevice, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"), false); btSocket.connect(); InputStream input = btSocket.getInputStream(); DataInputStream dinput = new DataInputStream(input); 

then later on when I want to read I use readFully:

dinput.readFully(byteArray, 0, byteArray.length); 

Источник

how to read from the InputStream of a bluetooth on Android

I am trying to test this bluetooth communication example between a PC and an Android phone. My SPP client is exactly the one from there and it works fine. I am new to Android and I didn’t want to make it run in a separate thread because I don’t know how, so I just did everything in the onCreate() method. If this is not the best way, feel free to point me to a better way, but this is not my main problem. The problem is I wanted to display the text received via bluetooth on a textView and I don’t know how to read from InputStream . When the code is left like that, it displays something like java.io.DataInputStream@41b0cb68 I tried it like here it didn’t display anything, also I don’t know what encoding is being used. here’s my android app’s code:

import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; import android.app.Activity; import android.bluetooth.*; import android.util.Log; import android.widget.RelativeLayout; import android.widget.TextView; public class MainActivity extends Activity < //based on java.util.UUID private static UUID MY_UUID = UUID.fromString("446118f0-8b1e-11e2-9e96-0800200c9a66"); // The local server socket private BluetoothServerSocket mmServerSocket; // based on android.bluetooth.BluetoothAdapter private BluetoothAdapter mAdapter; private BluetoothDevice remoteDevice; TextView text; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.textView_Text); BluetoothSocket socket = null; mAdapter = BluetoothAdapter.getDefaultAdapter(); // Listen to the server socket if we're not connected // while (true) < try < // Create a new listening server socket Log.d((String) this.getTitle(), ". Initializing RFCOMM SERVER. "); // MY_UUID is the UUID you want to use for communication mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord("MyService", MY_UUID); //mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID); // you can also try using In Secure connection. // This is a blocking call and will only return on a // successful connection or an exception socket = mmServerSocket.accept(); >catch (Exception e) < >try < Log.d((String) this.getTitle(), "Closing Server Socket. "); mmServerSocket.close(); InputStream tmpIn = null; OutputStream tmpOut = null; // Get the BluetoothSocket input and output streams tmpIn = socket.getInputStream(); tmpOut = socket.getOutputStream(); DataInputStream mmInStream = new DataInputStream(tmpIn); DataOutputStream mmOutStream = new DataOutputStream(tmpOut); // here you can use the Input Stream to take the string from the client whoever is connecting //similarly use the output stream to send the data to the client text.setText(mmInStream.toString()); >catch (Exception e) < //catch your exception here >// > > > 

I commented out the while(true) loop because I think it was making my app crash when onPause() was being called. I know this is not the best implementation but I really want to read from the bluetooth I feel like I am very close :), other aspects will be dealt with afterwards (like working with threads and so on).

Читайте также:  Bluetooth dialog es 15bt

Источник

How to send/receive messages via bluetooth android studio

I am trying to create an app that allows a string to be sent from one Android phone to another. The code for this is provided below. However, it isn’t working as I keep getting exceptions from the try catch piece of code under the pairDevice() section. Does anyone know why I might be getting this?

import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Intent; import android.os.ParcelUuid; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Scanner; import java.util.Set; public class MainActivity extends AppCompatActivity < InputStream inStream; OutputStream outputStream; private static final int REQUEST_ENABLE_BT = 1; public void pairDevice() < BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) < Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);>Set pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) < Object[] devices = pairedDevices.toArray(); BluetoothDevice device = (BluetoothDevice) devices[0]; ParcelUuid[] uuid = device.getUuids(); try < BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(uuid[0].getUuid()); socket.connect(); Toast.makeText(this, "Socket connected", Toast.LENGTH_LONG).show(); outputStream = socket.getOutputStream(); inStream = socket.getInputStream(); >catch (IOException e) < Toast.makeText(this, "Exception found", Toast.LENGTH_LONG).show(); >> > public void SendMessage(View v) < EditText outMessage = (EditText) findViewById(R.id.editText); try < if (outputStream != null) outputStream.write(outMessage.toString().getBytes()); TextView displayMessage = (TextView) findViewById(R.id.textView); Scanner s = new Scanner(inStream).useDelimiter("\\A"); displayMessage.setText(s.hasNext() ? s.next() : ""); >catch (IOException e) Toast.makeText(this,"No output stream", Toast.LENGTH_LONG).show(); > @Override protected void onCreate(Bundle savedInstanceState)

3 Answers 3

I have made few changes to your app:-

Firstly, I shifted the code responsible for creating the Bluetooth connection to ConnectThread .

2) Added AcceptThread responsible for listening incoming connections and ConnectedThread maintaining the BTConnection, Sending the data, and receiving incoming data through input/output streams respectively. 3) Created 2 buttons to start ConnectThread and AcceptThread.

NOTE: Make sure both the devices are paired and the device that you are trying to connect to is at the top of the list(or just remove all the paired devices from both the devices and only pair the devices that you want to connect). Also, you must start the AcceptThread before ConnectThread

MAINACTIVITY.JAVA

public class MainActivity extends AppCompatActivity < private static final UUID MY_UUID_INSECURE = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66"); private static final int REQUEST_ENABLE_BT = 1; BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); private BluetoothDevice mmDevice; private UUID deviceUUID; ConnectedThread mConnectedThread; private Handler handler; String TAG = "MainActivity"; EditText send_data; TextView view_data; StringBuilder messages; public void pairDevice(View v) < SetpairedDevices = bluetoothAdapter.getBondedDevices(); Log.e("MAinActivity", "" + pairedDevices.size() ); if (pairedDevices.size() > 0) < Object[] devices = pairedDevices.toArray(); BluetoothDevice device = (BluetoothDevice) devices[0]; //ParcelUuid[] uuid = device.getUuids(); Log.e("MAinActivity", "" + device ); //Log.e("MAinActivity", "" + uuid) ConnectThread connect = new ConnectThread(device,MY_UUID_INSECURE); connect.start(); >> private class ConnectThread extends Thread < private BluetoothSocket mmSocket; public ConnectThread(BluetoothDevice device, UUID uuid) < Log.d(TAG, "ConnectThread: started."); mmDevice = device; deviceUUID = uuid; >public void run() < BluetoothSocket tmp = null; Log.i(TAG, "RUN mConnectThread "); // Get a BluetoothSocket for a connection with the // given BluetoothDevice try < Log.d(TAG, "ConnectThread: Trying to create InsecureRfcommSocket using UUID: " +MY_UUID_INSECURE ); tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID_INSECURE); >catch (IOException e) < Log.e(TAG, "ConnectThread: Could not create InsecureRfcommSocket " + e.getMessage()); >mmSocket = tmp; // Make a connection to the BluetoothSocket try < // This is a blocking call and will only return on a // successful connection or an exception mmSocket.connect(); >catch (IOException e) < // Close the socket try < mmSocket.close(); Log.d(TAG, "run: Closed Socket."); >catch (IOException e1) < Log.e(TAG, "mConnectThread: run: Unable to close connection in socket " + e1.getMessage()); >Log.d(TAG, "run: ConnectThread: Could not connect to UUID: " + MY_UUID_INSECURE ); > //will talk about this in the 3rd video connected(mmSocket); > public void cancel() < try < Log.d(TAG, "cancel: Closing Client Socket."); mmSocket.close(); >catch (IOException e) < Log.e(TAG, "cancel: close() of mmSocket in Connectthread failed. " + e.getMessage()); >> > private void connected(BluetoothSocket mmSocket) < Log.d(TAG, "connected: Starting."); // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(mmSocket); mConnectedThread.start(); >private class ConnectedThread extends Thread < private final BluetoothSocket mmSocket; private final InputStream mmInStream; private final OutputStream mmOutStream; public ConnectedThread(BluetoothSocket socket) < Log.d(TAG, "ConnectedThread: Starting."); mmSocket = socket; InputStream tmpIn = null; OutputStream tmpOut = null; try < tmpIn = mmSocket.getInputStream(); tmpOut = mmSocket.getOutputStream(); >catch (IOException e) < e.printStackTrace(); >mmInStream = tmpIn; mmOutStream = tmpOut; > public void run() < byte[] buffer = new byte[1024]; // buffer store for the stream int bytes; // bytes returned from read() // Keep listening to the InputStream until an exception occurs while (true) < // Read from the InputStream try < bytes = mmInStream.read(buffer); final String incomingMessage = new String(buffer, 0, bytes); Log.d(TAG, "InputStream: " + incomingMessage); runOnUiThread(new Runnable() < @Override public void run() < view_data.setText(incomingMessage); >>); > catch (IOException e) < Log.e(TAG, "write: Error reading Input Stream. " + e.getMessage() ); break; >> > public void write(byte[] bytes) < String text = new String(bytes, Charset.defaultCharset()); Log.d(TAG, "write: Writing to outputstream: " + text); try < mmOutStream.write(bytes); >catch (IOException e) < Log.e(TAG, "write: Error writing to output stream. " + e.getMessage() ); >> /* Call this from the main activity to shutdown the connection */ public void cancel() < try < mmSocket.close(); >catch (IOException e) < >> > public void SendMessage(View v) < byte[] bytes = send_data.getText().toString().getBytes(Charset.defaultCharset()); mConnectedThread.write(bytes); >@Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); send_data =(EditText) findViewById(R.id.editText); view_data = (TextView) findViewById(R.id.textView); if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) < Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); >> public void Start_Server(View view) < AcceptThread accept = new AcceptThread(); accept.start(); >private class AcceptThread extends Thread < // The local server socket private final BluetoothServerSocket mmServerSocket; public AcceptThread()< BluetoothServerSocket tmp = null ; // Create a new listening server socket try< tmp = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("appname", MY_UUID_INSECURE); Log.d(TAG, "AcceptThread: Setting up Server using: " + MY_UUID_INSECURE); >catch (IOException e) < Log.e(TAG, "AcceptThread: IOException: " + e.getMessage() ); >mmServerSocket = tmp; > public void run()< Log.d(TAG, "run: AcceptThread Running."); BluetoothSocket socket = null; try< // This is a blocking call and will only return on a // successful connection or an exception Log.d(TAG, "run: RFCOM server socket start. "); socket = mmServerSocket.accept(); Log.d(TAG, "run: RFCOM server socket accepted connection."); >catch (IOException e) < Log.e(TAG, "AcceptThread: IOException: " + e.getMessage() ); >//talk about this is in the 3rd if(socket != null) < connected(socket); >Log.i(TAG, "END mAcceptThread "); > public void cancel() < Log.d(TAG, "cancel: Canceling AcceptThread."); try < mmServerSocket.close(); >catch (IOException e) < Log.e(TAG, "cancel: Close of AcceptThread ServerSocket failed. " + e.getMessage() ); >> > 

ACTIVITY_MAIN.XML

Читайте также:  Блютуз адаптер d link dbt 122

Источник

Bluetooth в Android Studio

Понадобилось мне как то приложение на телефон с OS Android, для работы с моим устройством на микроконтроллере через Bluetooth. И вот я уже погрузился в изучение Bluetooth в android studio.

Составим простой алгоритм работы:

  1. При запуске приложения на телефоне, отобразим все Bluetooth устройства, которые уже спарены с телефоном.
  2. Организуем поиск новых устройств.
  3. Далее, по нажатию на устройство в списке, сделаем спаривание с этим устройством ( конечно же не для того, что бы появились маленькие Bluetooth –ики, этого требует протокол работы bluetooth)
  4. Создадим класс для подключение устройств друг с другом по протоколу SPP.

Что ж, план намечен приступаем к действию. Так как статья получается довольно обширная, по этому 3 и 4 пункты опишем в следующей статье.

Включение Bluetooth в телефоне

Что бы начать работать с Bluetooth первым делом его надо включить. Рассмотрим функцию реализующую проверку на включение Bluetooth, и если выключен, предлагает пользователю включить его.

public boolean BluetoothON() < if (!bleAdapter.isEnabled()) < Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) < return false; >startActivity(turnOn); > else < return true; >return false; >

Здесь, первым делом мы проверяем есть ли у нас в системе Bluetooth адаптер. Если есть, создаем intent с действием на включение блютуза. Так же нам надо проверить есть ли разрешение на работу с блютуз в манифесте. Результатом работы кода будет следующий рисунок.

android studio bluetooth on

Bluetooth в Android Studio: показываем сопряженные устройства

Рассмотрим данный вопрос на примере приведенного ниже кода.

public void GetPairDevise(ArrayList list, BluetoothAdapter bluetoothAdapter) < if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) < // Выдать сообщение о ошибке return; >Set pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) < for (BluetoothDevice device : pairedDevices) < arrayNewDevice.add(device); list.add(device.getName() + " - " + device.getAddress()); >> >

И так, функция принимает на вход переменную типа ArrayList. В этот массив мы помещаем найденные в системе устройства.

Читайте также:  Bluetooth for hp 630

Вторым параметром наш BluetoothAdapter. По сути это наш физический модуль Bluetooth в телефоне. Узнаем о его присутствии через функцию:

getDefaultAtapter();

BluetoothAdapter bleAdapter = BluetoothAdapter.getDefaultAdapter();

Далее необходимо проверить, есть ли необходимые разрешение в нашем файле Манифесте. Без них Bluetooth работать не будет.

getBondedDevices(),

получаем все сопряженные устройства.

Set pairedDevices = bluetoothAdapter.getBondedDevices();

Далее в цикле проходим по всему сету и добавляем наши устройства в массив для дальнейшей работы и в список отображение. На картинке ниже, результат работы нашей функции.

Bluetooth pair

Bluetooth в android studio: поиск устройств

Поиск новых устройств запускает функция bluetoothAdapter.startDiscovery() . Результат работы будет в обработчике событий BroadcastResiver . Это класс в котором идет обработка сообщений.

Посмотрим нижеприведенный код. Сначала мы очистим наши списки. Далее проверим разрешение в манифесте и запустим функцию bluetoothAdapter.startDiscovery(). Инициируем два события: поиск устройств и окончание поиска устройств. После этого регистрируем каждое событие Bluetooth.

public void GetScanDevise(BluetoothAdapter mYBluetoothAdapter) < if (listNewDeviceAdapter.getCount() != 0) listNewDeviceAdapter.clear(); arrayNewDevice.clear(); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) < return; >myBleAdapter.startDiscovery(); IntentFilter myFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mResiver, myFilter); myFilter = new IntentFilter((BluetoothAdapter.ACTION_DISCOVERY_FINISHED)); registerReceiver(mResiver, myFilter); > private final BroadcastReceiver mResiver = new BroadcastReceiver() < @SuppressLint("MissingPermission") @Override public void onReceive(Context context, Intent intent) < String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) < myDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (myDevice.getName() != null) < arrayNewDevice.add(myDevice); deviseScanList.add(myDevice.getName() + " - " + myDevice.getAddress()); listDevice.setAdapter(listNewDeviceAdapter); >> else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) < myBleAdapter.cancelDiscovery(); >> >;

Внимание! Что бы вновь начать поиск новых устройств, необходимо убедиться, что предыдущий поиск остановлен.

Также можно перед стартом нового поиска сначала вызвать функцию

bleAdapter.cancelDiscovery();

Результат работы кода будет представлен на картинке ниже.

Bluetooth scan

Внимание! Данный программный код представлен из реально работающей программы. Описывал все так как понял сам, читая открытые источники. К сожалению я не являюсь программистом Android, по этому говорю честно, данную задачу можно реализовать красивым и правильным кодом. В этой статье описан просто рабочий код и принцип работы с Bluetooth в android studio на языке java.

Источник

Оцените статью
Adblock
detector