我的应用程序:/***连接蓝牙手机。*自动打开蓝牙,按钮关闭蓝牙*/
package com.Iris;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import java.util.Set;
public class IrisActivity extends Activity
{
Button button1;
// button Off bluettooch
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// ac
bluetoothAdapter.enable();
Toast.makeText(IrisActivity.this, "Bluetooth Activé", Toast.LENGTH_SHORT).show();
Set<BluetoothDevice> devices;
devices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice blueDevice : devices)
{
Toast.makeText(IrisActivity.this, "Réseau : " + blueDevice.getName(), Toast.LENGTH_SHORT).show();
}
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
bluetoothAdapter.disable();
Toast.makeText(IrisActivity.this, "Bluetooth Désactivé", Toast.LENGTH_SHORT).show();
}
});
}
我的应用程序很好,但我想连接到"blueDevice.getName",并将ASCI字母"H"与我的应用一起发送
你能帮我吗?
查看BluetoothChat示例。基本上,您需要在传输端执行以下操作:
BluetoothSocket socket = blueDevice.connect();
OutputStream os = socket.getOutputStream();
os.write("H");
您将需要一个位于接收端的服务器套接字,如示例所示。