2013-09-30 6 views
0

블루투스를 통해 패스 메시지 용 앱을 개발 중입니다. 한 장치에서 다른 장치로 하나의 메시지를 전달하고 싶습니다 (이미 페어링 된 장치입니다). 페어링 된 장치를 표시 할 수 있습니다. 두 장치를 연결합니다. 누구나 따라야 할 단계가 무엇인지 말해 줄 수 있습니까? 두 대의 전화 사이에 연결을 만드는 방법은 무엇입니까?안드로이드 메시지 블루투스를 통해 패스

 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; 
} 

} 대신 textView을 사용

답변

0

, 목록보기 사용하고 당신이 textView로 추가 한 동일한 방식으로 items를 추가합니다.

//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. 



} 

, 당신의 메시지가 비명을 전송 듣고하는 서버를 설정합니다 이는 다음과 같습니다 방법이있다. 그 서버 (서버로 설정, 내 말을 그대로 사용하지 마십시오)됩니다.
전에 앱이 스레드를 사용하여 서버에 연결되어 있는지 확인하십시오. 당신은 안드로이드 스레드에 대해 더 많은 것을 읽을 수 있습니다. 서버에 연결 (서버로 작동하는 다른 앱), 매니 페스트 파일의 안드로이드 권한 (예 : 블루투스 및 관리자)이 중요합니다. 두 가지 방법으로 메시지를 전달하는 양방향 통신 앱은 신이 금지합니다. 그러면 두 앱 모두에서 서버와 클라이언트를 처리하는 데 동일한 코딩 작업을 수행해야합니다.