2016-05-04 147 views
0

私はAndroidの初心者です。 Bluetoothのオン/オフ(別のボタン)、近くのデバイスのスキャン(別のボタンの使用)、ペアデバイスの表示(別のボタン)のBluetoothモジュールを作成しました。ここで、スキャンをクリックすると、リストされた各デバイスの横に小さなペアボタンと接続ボタンを含むデバイスのリストが表示されます。Bluetoothを切断するAndroidコード

私は両方のコードを記述して接続して正常に動作します。もう一度同じ接続ボタンをクリックして接続を切断します。指定したデバイスに接続するとすぐに、接続ボタン内のテキストに「切断」が表示されます。私が接続をクリックすると、それはデバイスに接続していますが、切断していません。

package net.londatiga.android.bluetooth; 

import java.util.List; 

import android.bluetooth.BluetoothDevice; 
import android.content.Context; 
import android.drm.DrmStore.Action; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.TextView; 


public class DeviceListAdapter extends BaseAdapter{ 
    private LayoutInflater mInflater; 
    private List<BluetoothDevice> mData; 
    private OnPairButtonClickListener mListener; 
    private String str; 
    Button btnSend; 
    Connect con=null; 
    Button connectbtn; 
    //ProgressDialog mConnectingDlg; 



    public DeviceListAdapter(Context context) { 
     mInflater = LayoutInflater.from(context);   
    } 

    public void setData(List<BluetoothDevice> data) { 
     mData = data; 
    } 

    public void setListener(OnPairButtonClickListener listener) { 
     mListener = listener; 
    } 

    public int getCount() { 
     return (mData == null) ? 0 : mData.size(); 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 




    public View getView(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 


     if (convertView == null) {   
      convertView   = mInflater.inflate(R.layout.list_item_device, null); 

      holder    = new ViewHolder();   
      holder.nameTv  = (TextView) convertView.findViewById(R.id.tv_name); 
      holder.addressTv = (TextView) convertView.findViewById(R.id.tv_address); 
      holder.pairBtn  = (Button) convertView.findViewById(R.id.btn_pair); 
      holder.connectbtn  = (Button) convertView.findViewById(R.id.btn_connect); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     final BluetoothDevice device = mData.get(position); 

     holder.nameTv.setText(device.getName()); 
     holder.addressTv.setText(device.getAddress()); 
     holder.pairBtn.setText((device.getBondState() == BluetoothDevice.BOND_BONDED) ? "Unpair" : "Pair"); 
     holder.pairBtn.setOnClickListener(new View.OnClickListener() {   
      @Override 
      public void onClick(View v) { 
       if (mListener != null) { 
        mListener.onPairButtonClick(position); 
       } 
      } 
     }); 


      // holder.connectbtn.setText((device.getBondState() == BluetoothDevice.BOND_BONDED) ? "Disonnect" : "connect"); 
      holder.connectbtn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       String s=holder.connectbtn.getText().toString(); 

     Connect con=new Connect(new Connect.P25ConnectionListener() { 

     @Override 
     public void onStartConnecting() { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onDisconnected() { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onConnectionSuccess() { 
      // TODO Auto-generated method stub 


     } 

     @Override 
     public void onConnectionFailed(String error) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onConnectionCancelled() { 
      // TODO Auto-generated method stub 

     } 
    }); 
     con.connect(device); 
    System.out.println("Device is connected");  



      } 










      }); 




     return convertView; 
} 

    static class ViewHolder { 
     Button connectbtn; 
     TextView nameTv; 
     TextView addressTv; 
     Button pairBtn; 
    } 

    public interface OnPairButtonClickListener { 
     public abstract void onPairButtonClick(int position); 
    } 




    } 
+0

を無効にしたい場合は、私はこのプロジェクトを見てみることをおアドバイスhttps://github.com/Fakher-Hakim/android-BluetoothLeGatt これは、使用する方法についてのGoogleのサンプルプロジェクトに基づいていますAndroidアプリケーションでは不可能です。 – Fakher

答えて

1
//Get bluetooth adapter 
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

あなたはこのブール

boolean bluetoothEnabled = mBluetoothAdapter.isEnabled(); 
てボタンの状態(切断/ conncted)を変更するアプリを開いたときの状態を確認したい:ここ

は、サンプルコードです

ブルートゥースを有効にする場合

mBluetoothAdapter.enable(); 

あなたはブルートゥース

mBluetoothAdapter.disable(); 
関連する問題