2014-03-04 25 views
0

这里蓝牙配对是我的问题/问题,可能是一个nooby ....与所选设备

我有当应用程序检查蓝牙开启的活动,如果没有它会显示一个弹出窗口用一个按钮来激活它。

当激活时有一个按钮,它将搜索蓝牙设备。

现在我的问题是如何能够配对,这将有密码保护的所选择的设备,如果已经配对只是连接到它(witouth再次输入密码)

连接到该设备之后应该转到下一页。

这里是我的代码

package com.example.silcamanager96x32; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView.OnItemClickListener; 

public class Bluetooth_check extends Activity { 

    //global variables 
    private final static int REQUEST_ENABLE_BT = 1; 
    private ArrayAdapter<String> btArrayAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bluetooth_check); 

     btn_go(); 
     btn_scan(); 

    } 

    //tijdelijke button volgende pagina 
    public void btn_go(){ 
     Button btn_go=(Button)findViewById(R.id.Button01); 
     btn_go.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
        Log.i("clicks","You Clicked B1"); 
       Intent i=new Intent(
        Bluetooth_check.this, 
        home.class); 
       startActivity(i); 
      } 
     }); 
    } 

    //button scannen naar bluetooth apparaten en in ene lijst plaatsen 
    public void btn_scan(){ 
     final Button scanb = (Button)findViewById(R.id.button); 
     final ListView Deviceslist = (ListView)findViewById(R.id.listView1); 
     btArrayAdapter = new ArrayAdapter<String>(Bluetooth_check.this, android.R.layout.simple_list_item_1);  
     Deviceslist.setAdapter(btArrayAdapter); 

     //vraagt of app bluetooth mag inschakelen 
     final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (mBluetoothAdapter == null) { 
      // Device does not support Bluetooth 
      Toast.makeText(Bluetooth_check.this, "Your device doesnot support Bluetooth", Toast.LENGTH_LONG).show(); 
     } 

     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 

     //onclick scan button 
     scanb.setOnClickListener(new OnClickListener() 
     { 
     public void onClick(View v) 
      { 
      btArrayAdapter.clear(); 
      mBluetoothAdapter.startDiscovery(); 
      Toast.makeText(Bluetooth_check.this, "Scanning Devices", Toast.LENGTH_LONG).show(); 

      } 
     }); 

     Deviceslist.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View v, int position, 
        long id) { 
       // onclick item gevonden apparaten 

        AlertDialog.Builder adb = new AlertDialog.Builder(
        Bluetooth_check.this); 
        adb.setTitle("ListView OnClick"); 
        adb.setMessage("Selected Item is = " 
        + Deviceslist.getItemAtPosition(position)); 
        adb.setPositiveButton("Ok", null); 
        adb.show();      
           } 

     }); 

     registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 
    } 

    @Override 
    protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    unregisterReceiver(FoundReceiver); 
    } 

    private final BroadcastReceiver FoundReceiver = new BroadcastReceiver(){ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     String action = intent.getAction(); 
     if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
     BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
     btArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
     btArrayAdapter.notifyDataSetChanged();   
     } 
    }}; 


} 

回答