2013-09-30 189 views
0

我正在開發一個通過bluetooth.I傳遞消息的應用程序我想從一個設備傳遞一個消息到其他設備(設備已配對)我能夠顯示配對devices.But我不知道如何連接兩個設備。任何人都可以告訴我應該遵循哪些步驟。如何在兩部手機之間創建連接?通過藍牙的Android MessagePassing

 public class MainActivity extends Activity { 
    TextView textview1; 
    private static final int REQUEST_ENABLE_BT = 1; 
    BluetoothAdapter btAdapter; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textview1 = (TextView) findViewById(R.id.textView1); 

    // Getting the Bluetooth adapter 
    btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    textview1.append("\nAdapter: " + btAdapter); 

    CheckBluetoothState(); 
    } 

    /* It is called when an activity completes.*/ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == REQUEST_ENABLE_BT) { 
     CheckBluetoothState(); 
    } 
    } 

    @Override 
    protected void onDestroy() { 
    super.onDestroy(); 
    } 

    private void CheckBluetoothState() { 
    // Checks for the Bluetooth support and then makes sure it is turned on 
    // If it isn't turned on, request to turn it on 
    // List paired devices 
    if(btAdapter==null) { 
     textview1.append("\nBluetooth NOT supported. Aborting."); 
     return; 
    } else { 
     if (btAdapter.isEnabled()) { 
     textview1.append("\nBluetooth is enabled..."); 

     // Listing paired devices 
     textview1.append("\nPaired Devices are:"); 
     Set<BluetoothDevice> devices = btAdapter.getBondedDevices(); 
     for (BluetoothDevice device : devices) { 
      textview1.append("\n Device: " + device.getName() + ", " + device); 
     } 
     } else { 
     //Prompt user to turn on Bluetooth 
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 
    } 
    } 


@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

而不是使用textView,使用ListView和添加items它,你將他們添加到textView相同的方式。

//declaration in class 
ListView lview; 
ArrayAdapter<String> listAdapter; 

//in onCreate() 

lview = (ListView) findViewById(R.id.listPairedDev); 
lview.setOnItemClickListener(this); 

///////here it gets added to list 
ArrayOfDevices = btAdapter.getBondedDevices(); 
        if(ArrayOfDevices.size()>0)//paired dev more than 0 
        { 
         for(BluetoothDevice device: ArrayOfDevices) 
         { 
          listAdapter.add(device.getName()+ "\n" +device.getAddress()); 

         } 
        } 

並閱讀有關如何添加匿名onClickListener或動作偵聽器的類型。 這有方法,它看起來是這樣的:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 


//Click event on individual item of list. 



} 

當你設置單擊事件,設置哪些會聽你的消息發送尖叫的服務器。這將是一個服務器(設置爲服務器,不要把我的話,因爲它們)。
之前,確保您的應用程序連接到服務器,使用線程。你可以閱讀更多關於android線程。連接到服務器(其他應用程序充當服務器),Android清單文件中的權限,如藍牙和管理員很重要。上帝禁止它是一個雙向通信應用程序,傳遞雙向消息,那麼你必須在兩個應用程序中執行相同的代碼來處理服務器以及客戶端。