我正在写一个android程序,我需要将一个角色发送到另一个蓝牙设备。我已经编写了这段程序,但是当你发送角色时,我不知道我使用的是什么功能。在该计划中,该党对点数审讯存在疑问。
package com.example.bl;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.view.*;
import android.app.*;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Handler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import java.io.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bot1 = (Button) findViewById(R.id.bt1);
bot1.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
int REQUEST_ENABLE_BT = 1;
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
// ??????
// ??????
// ??????
// ??????
AlertDialog.Builder dialogo = new
AlertDialog.Builder(MainActivity.this);
dialogo.setTitle("Aviso");
dialogo.setMessage("Foi!");
dialogo.setNeutralButton("OK", null);
dialogo.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:0)
首先从android docs
中查看此页蓝牙使用android.bluetooth.BluetoothSocket
建立连接
要创建用于连接已知设备的BluetoothSocket,请使用BluetoothDevice.createRfcommSocketToServiceRecord().
然后调用connect()
尝试连接到远程设备。此呼叫将阻塞,直到建立连接或连接失败。