如何将数据(字符串)发送到 android 中的配对设备? [英] how to send data (a string) to a paired device in android?

查看:33
本文介绍了如何将数据(字符串)发送到 android 中的配对设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用蓝牙.我得到了配对设备的列表.现在我的要求是我需要向设备发送一些数据(一个字符串).我怎样才能做到这一点?我尝试搜索,但没有找到任何有用的东西.有人可以帮忙吗?

I am working on bluetooth for the first time. I got the list of paired devices. Now my requirement is I need to send some data (a string) to the device. how can I do that? I tried searching but didn't find anything useful. Could anyone help out this?

推荐答案

这样的事情可能就足够了:

Something like this might suffice:

DataOutputStream os;

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

BroadcastReceiver discoveryResult = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String remoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
        BluetoothDevice remoteDevice;

        remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        Toast.makeText(getApplicationContext(), "Discovered: " + remoteDeviceName + " address " + remoteDevice.getAddress(), Toast.LENGTH_SHORT).show();

        try{
            BluetoothDevice device = bluetooth.getRemoteDevice(remoteDevice.getAddress());

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

            BluetoothSocket clientSocket =  (BluetoothSocket) m.invoke(device, 1);

            clientSocket.connect();

            os = new DataOutputStream(clientSocket.getOutputStream());

            new clientSock().start();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("BLUETOOTH", e.getMessage());
        }
    }
};

registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));

bluetooth.enable();
if (!bluetooth.isDiscovering()) {
    bluetooth.startDiscovery();
}

public class clientSock extends Thread {
    public void run () {
        try {
            os.writeBytes("anything you want"); // anything you want
            os.flush();
        } catch (Exception e1) {
            e1.printStackTrace();
            return;
        }
    }
}

您还需要大量导入,例如:

You will also need a lot of imports such as these:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import java.util.UUID;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

请注意,对于此示例代码,并非所有导入都是必需的,您的 IDE 可能会帮助您整理它们.

note that not all imports are necessary for this example code, your IDE might help you to sort them out for you.

os.writeBytes("anything you want") 上传递数据;//任何你想要的 行.

您还需要权限

这篇关于如何将数据(字符串)发送到 android 中的配对设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆