这个问题已经解决了!非常感谢布拉德,丹尼斯和瘾君子!你是英雄!:)
这是工作代码.它连接到Zeemote并从中读取数据.
public class ZeeTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
for (int i = 0; i < 3; i++) {
test();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean connected = false;
private BluetoothSocket sock;
private InputStream in;
public void test() throws Exception {
if (connected) {
return;
}
BluetoothDevice zee = BluetoothAdapter.getDefaultAdapter().
getRemoteDevice("00:1C:4D:02:A6:55");
Method m = zee.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
sock = (BluetoothSocket)m.invoke(zee, Integer.valueOf(1));
Log.d("ZeeTest", … 我正在开发一个应用程序,我想连接蓝牙设备主要问题是我不希望用户输入所需的引脚而不是应用程序应该自己做...我没有任何连接相关的问题...只想要按应用程序本身插入并完成引脚认证过程.
我发现以下代码我确信它正在工作但不确定如何在此代码中添加引脚?
private void pairDevice(BluetoothDevice device) {
try {
Log.d("pairDevice()", "Start Pairing...");
Method m = device.getClass().getMethod("createBond", (Class[]) null);
m.invoke(device, (Object[]) null);
Log.d("pairDevice()", "Pairing finished.");
} catch (Exception e) {
Log.e("pairDevice()", e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何在上面的代码或任何类似的代码中输入pin来解决问题..谢谢
我已经发现了其他设备,我已经配对了它.至少我把它放在Android手机上的配对设备列表中.
现在在BluetoothSocket.connect()上可能会出现两个问题:
远程设备忘记了配对,因为它只能配对另一台设备,并且已与另一部手机配对
=>然后在一定的超时后连接失败.
是否可以检查已配对的设备是否真的可用,并记住它是否与我的手机配对而没有连接到它?这与检测设备是否已连接无关.配对和可见与连接不同.
我通过我的Android手机单元将文本发送到蓝牙打印机.无论是打印机和我的设备通过蓝牙连接. 它工作正常,我在纸上得到了所需的文字.
我的问题是:
打印机采用默认font size的文本.我想更改要打印的文本的字体大小.
我怎么能实现这个?
这是我在bloutooth连接后打印文本的代码:
private void connect_print(BluetoothDevice bluetoothDevicess) {
// some code
printData();
// some code
}
Run Code Online (Sandbox Code Playgroud)
printData()方法
private void printData() {
// TODO Auto-generated method stub
String str = new String("This is the text sending to the printer");
String newline = "\n";
try {
out.write(str.getBytes(),0,str.getBytes().length);
Log.i("Log", "One line printed");
} catch (IOException e) {
Toast.makeText(BluetoothDemo.this, "catch 1", Toast.LENGTH_LONG).show();
e.printStackTrace();
Log.i("Log", "unable to write ");
flagCheck = false;
}
try {
out.write(newline.getBytes(),0,newline.getBytes().length);
} catch …Run Code Online (Sandbox Code Playgroud) 我需要从我的Android应用程序建立与obd2设备的连接,如扭矩app.I需要从Android手机配对.
任何人都能用简单的话语解释我在Android蓝牙示例中需要UUID.我已经阅读了一些关于它的文章,但仍然没有明确UUID的确切需求.现在让我向您解释我想要开发的场景:我想开发一个Android应用程序,用于将数据从我的手机传输到另一部手机通过蓝牙传输,例如"带有.xyz扩展名的文件".接收电话也不一定非常需要我使用的应用程序.我只是想将数据从我的应用程序传输到其他手机,就是这样.我不关心接收器对数据做了什么.我只想连接到范围内的设备并使用我的应用程序传输该文件现在我应该怎么做?UUID的角色来自哪里?我已经读过UUID是我的应用程序,服务器和接收器都应该知道这个UUID以形成连接.但是,如果接收器没有我的应用程序怎么办?它肯定不会知道我的应用程序UUID?那么数据传输将如何实现?我只是想在不涉及特定应用的情况下使用蓝牙.在这里,我的应用程序应该做什么?它应该是创建服务器套接字/客户端套接字还是什么?为什么
用简单的词语解释(如果可能的话,有些文章).我不想要有BluetoothChat建议的常规答案.如果你不明白这个问题请告诉我,我会尽量更具体,并为你详细说明.这个问题的基本目标是澄清UUID的使用,并在一个Android手机上运行的应用程序中使用蓝牙从两个设备(而不是应用程序)之间传输数据.
在我的应用程序中,我需要配对蓝牙设备并立即与它连接.
我有以下功能,以配对设备:
public boolean createBond(BluetoothDevice btDevice)
{
try {
Log.d("pairDevice()", "Start Pairing...");
Method m = btDevice.getClass().getMethod("createBond", (Class[]) null);
Boolean returnValue = (Boolean) m.invoke(btDevice, (Object[]) null);
Log.d("pairDevice()", "Pairing finished.");
return returnValue;
} catch (Exception e) {
Log.e("pairDevice()", e.getMessage());
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我用它如下:
Boolean isBonded = false;
try {
isBonded = createBond(bdDevice);
if(isBonded)
{
//Connect with device
}
}
Run Code Online (Sandbox Code Playgroud)
它显示了配对设备和输入引脚的对话框.
问题是createBond函数总是返回true,它一直等到我输入引脚并与设备配对,所以我没有正确使用:
isBonded = createBond(bdDevice);
if(isBonded) {...}
Run Code Online (Sandbox Code Playgroud)
所以问题是我如何与设备配对以及何时配对连接?
PD我的代码基于以下线程的第一个答案:Android +配对设备通过蓝牙编程
我正在创建一个应通过蓝牙连接到特定设备的应用程序。
我希望我的应用程序与此设备连接,无论它是否已配对。
现在我有这个
private void findDevice() {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals(DEVICE_NAME)) {
bluetoothDevice = device;
deviceFound = true;
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但此功能仅连接到配对设备。如果我的设备尚未配对,我想配对它。不知道该怎么做。
有人可以给我任何建议吗?