我正在尝试开发一个Android应用程序,通过蓝牙远程控制Windows 7笔记本电脑上的鼠标。
我有一个C#WinForm是服务器而Android平板电脑是客户端。通过接受服务器和客户端上的连接,我能够成功地将它们配对。
问题是我希望它们自动配对,而服务器不需要手动接受配对。
过去3周我一直被困在这个位置,而截止日期是本周五。
在一些专家的帮助下拼命恳求:(
编辑:
C#:
public partial class MainForm : Form
{
BluetoothClient clientBluetooth;
BluetoothListener serverBluetooth;
Guid mUUID = new Guid("{00001101-0000-1000-8000-00805F9B34FB}");
public MainForm()
{
InitializeComponent();
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add(0,
new MenuItem("Quit", new System.EventHandler(quit_Click)));
notifyIcon1.ContextMenu = trayMenu;
notifyIcon1.MouseDoubleClick += notifyIcon1_MouseDoubleClick;
clientBluetooth = new BluetoothClient();
serverBluetooth = new BluetoothListener(mUUID);
serverBluetooth.Start();
}
}
机器人:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setup the window
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.device_list);
// Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
// Register for broadcasts when discovery has finished
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
// Invoke when a Bluetooth device is found
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When a remote Bluetooth device is found
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle("Select a device to connect");
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = "No device found".toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
}
};
我也试过这个:
private void clientConnect()
{
BluetoothDeviceInfo[] pairedDevicesList = clientBluetooth.DiscoverDevices();
foreach (BluetoothDeviceInfo device in pairedDevicesList)
{
if (!clientBluetooth.Connected)
{
BluetoothSecurity.PairRequest(device.DeviceAddress, "123456");
}
}
}
但这也行不通。蓝牙连接从“正在收听...”变为“正在连接...”并返回“正在收听...”