日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android ble从设备,从Android设备发送命令到蓝牙

發(fā)布時間:2025/3/15 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android ble从设备,从Android设备发送命令到蓝牙 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我發(fā)現(xiàn)命令從我的Android設(shè)備發(fā)送到藍牙設(shè)備。

藍牙與微控制器相關(guān)聯(lián)。我的努力如下:

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_ENABLE_BT = 2;

private BluetoothAdapter mBluetoothAdapter;

public static final String TAG = "CustomPOC BLEEEEEEE";

private Button btn_send;

private BluetoothDevice mdevice;

private Handler mHandler;

private ConnectThread mConnectThread;

private ConnectedThread mConnectedThread;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

btn_send = (Button)findViewById(R.id.senddata);

if (mBluetoothAdapter == null) {

Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();

finish();

return;

}

if (!mBluetoothAdapter.isEnabled()) {

Log.i(TAG, "onClick - BT not enabled yet");

Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableIntent, REQUEST_ENABLE_BT);

}

pairedOrNot();

btn_send.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mConnectThread = new ConnectThread(mdevice);

mConnectThread.start();

// new ConnectAsynk(mdevice).execute();

}

});

}

private void pairedOrNot() {

Set pairedDevices = mBluetoothAdapter.getBondedDevices();

List s = new ArrayList();

for(BluetoothDevice bt : pairedDevices) {

s.add(bt.getAddress());

s.add(bt.getName());

if("08:7C:BE:00:00:01".equals(bt.getAddress())) {

mdevice = bt;

}

}

}

@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) {

case REQUEST_ENABLE_BT:

// When the request to enable Bluetooth returns

if (resultCode == Activity.RESULT_OK) {

Toast.makeText(this, "Bluetooth has turned on ", Toast.LENGTH_SHORT).show();

} else {

// User did not enable Bluetooth or an error occurred

Log.d(TAG, "BT not enabled");

Toast.makeText(this, "Problem in BT Turning ON ", Toast.LENGTH_SHORT).show();

finish();

}

break;

default:

Log.e(TAG, "wrong request code");

break;

}

}

private class ConnectThread extends Thread {

private final BluetoothSocket mmSocket;

private final BluetoothDevice mmDevice;

private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

public ConnectThread(BluetoothDevice device) {

BluetoothSocket tmp = null;

mmDevice = device;

try {

tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

/*Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});

tmp = (BluetoothSocket) m.invoke(device, 1);*/

System.out.println("BTTTTTTTTTTT Address "+mmDevice.getAddress());

BluetoothDevice hxm = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mmDevice.getAddress());

// Method m;

// m = hxm.getClass().getMethod("createRfcommSocket", new Class[]{int.class});

// tmp = (BluetoothSocket)m.invoke(hxm, Integer.valueOf(1));

}

catch (Exception e){

e.printStackTrace();

}

mmSocket = tmp;

}

public void run() {

mBluetoothAdapter.cancelDiscovery();

try {

mmSocket.connect();

// Reset the ConnectThread because we're done

synchronized (MainActivity.this) {

mConnectThread = null;

}

// Cancel the thread that completed the connection

if (mConnectThread != null) {

mConnectThread.cancel();

mConnectThread = null;

}

// Cancel any thread currently running a connection

if (mConnectedThread != null) {

mConnectedThread.cancel();

mConnectedThread = null;

}

ConnectedThread mConnectedThread = new ConnectedThread(mmSocket);

mConnectedThread.start();

} catch (IOException connectException) {

try {

connectException.printStackTrace();

mmSocket.close();

} catch (IOException closeException) { }

} catch (Exception ex){

ex.printStackTrace();

}

}

public void cancel(){

try {

mmSocket.close();

} catch (IOException e) { e.printStackTrace();}

}

}

private class ConnectedThread extends Thread {

private final BluetoothSocket mmSocket;

private final InputStream mmInStream;

private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {

mmSocket = socket;

InputStream tmpIn = null;

OutputStream tmpOut = null;

try {

tmpIn = socket.getInputStream();

tmpOut = socket.getOutputStream();

} catch (IOException e) { e.printStackTrace();}

mmInStream = tmpIn;

mmOutStream = tmpOut;

}

public void run() {

byte[] buffer = new byte[1024];

int begin = 0;

int bytes = 0;

while (true) {

try {

bytes += mmInStream.read(buffer, bytes, buffer.length - bytes);

for(int i = begin; i < bytes; i++) {

if(buffer[i] == "1010101100000001000000100000000100001110".getBytes()[0]) {

mHandler.obtainMessage(1, begin, i, buffer).sendToTarget();

begin = i + 1;

if(i == bytes - 1) {

bytes = 0;

begin = 0;

}

}

}

} catch (IOException e) {

e.printStackTrace();

break;

}

}

}

public void write(byte[] bytes) {

try {

mmOutStream.write(bytes);

} catch (IOException e) {

e.printStackTrace();

}

}

public void cancel() {

try {

mmSocket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

當(dāng)我檢查連接狀態(tài)時,幾個單挑

tmp.isConnected()

我發(fā)現(xiàn)它正在返回

false

值。

我要將此命令(1010101100000001000000010000000100001110)發(fā)送到外部藍牙。但我正在收到問題。日志跟蹤在下面的應(yīng)用程序啟動時間:

08-17 07:48:39.718: W/art(14551): Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.

drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList,

android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable

當(dāng)點擊按鈕btn_send,然后我在日志追蹤中得到以下消息:

08-17 07:51:32.046: W/BluetoothAdapter(14984): getBluetoothService() called with no BluetoothManagerCallback

08-17 07:51:38.448: W/System.err(14984): java.io.IOException: read failed, socket might closed or timeout, read ret: -1

08-17 07:51:38.449: W/System.err(14984): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573)

08-17 07:51:38.449: W/System.err(14984): at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:584)

08-17 07:51:38.449: W/System.err(14984): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:321)

08-17 07:51:38.449: W/System.err(14984): at com.mahmad.custompoc11aug.MainActivity$ConnectThread.run(MainActivity.java:164)

在調(diào)試之后,我觀察到這個問題在這一行

mmSocket.connect();

所有必需的權(quán)限都在清單文件中給出。請幫我解決這個問題。提前致謝。

總結(jié)

以上是生活随笔為你收集整理的android ble从设备,从Android设备发送命令到蓝牙的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。