2016-09-30 16 views
3
  • 私はPDFを作成することができ、またBluetoothプリンタを使用して印刷することができるアプリケーションに取り組んでいます。
    • 私はPDFを作成しましたが、Bluetoothプリンタを使用して印刷機能を統合しようとすると、そこにエラーが発生します。
    • Bluetoothデバイスのリストを取得できませんでした。
    • サンプルコードがあれば、それを私に提供してください。それは私にはあまりにも重要です。

これは私のコードアンドロイドでBluetoothプリンタを使用して印刷する

です....

ありがとうPrintActivity.java

public class PrintActivity extends Activity implements Runnable { 

int idd; 

protected static final String TAG = "TAG"; 
private static final int REQUEST_CONNECT_DEVICE = 1; 
private static final int REQUEST_ENABLE_BT = 2; 

Button btnPrinr; 
BluetoothAdapter mBluetoothAdapter; 

private UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
private ProgressDialog mBluetoothConnectProgressDialog; 
private BluetoothSocket mBluetoothSocket; 
BluetoothDevice mBluetoothDevice; 

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

    idd = getIntent().getIntExtra("ID", 0); 
    Log.e("ID", "" + idd); 

    btnPrinr = (Button) findViewById(R.id.btnPrinr); 

    btnPrinr.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
      if (mBluetoothAdapter == null) { 
       Toast.makeText(PrintActivity.this, "Message1", Toast.LENGTH_SHORT).show(); 
      } else { 
       if (!mBluetoothAdapter.isEnabled()) { 
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
       } else { 
        ListPairedDevices(); 
        Intent connectIntent = new Intent(PrintActivity.this, DeviceListActivity.class); 
        startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE); 
       } 
      } 
     } 
    }); 

} 

public void onActivityResult(int mRequestCode, int mResultCode, Intent mDataIntent) { 
    super.onActivityResult(mRequestCode, mResultCode, mDataIntent); 

    switch (mRequestCode) { 
     case REQUEST_CONNECT_DEVICE: 
      if (mResultCode == Activity.RESULT_OK) { 
       Bundle mExtra = mDataIntent.getExtras(); 
       String mDeviceAddress = mExtra.getString("DeviceAddress"); 
       Log.v(TAG, "Coming incoming address " + mDeviceAddress); 
       mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress); 
       mBluetoothConnectProgressDialog = ProgressDialog.show(this, "Connecting...", mBluetoothDevice.getName() + " : " + mBluetoothDevice.getAddress(), true, false); 
       Thread mBlutoothConnectThread = new Thread(this); 
       mBlutoothConnectThread.start(); 
       //pairToDevice(mBluetoothDevice); This method is replaced by progress dialog with thread 
      } 
      break; 

     case REQUEST_ENABLE_BT: 
      if (mResultCode == Activity.RESULT_OK) { 
       ListPairedDevices(); 
       Intent connectIntent = new Intent(PrintActivity.this, DeviceListActivity.class); 
       startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE); 
      } else { 
       Toast.makeText(PrintActivity.this, "Message", Toast.LENGTH_SHORT).show(); 
      } 
      break; 
    } 
} 

private void ListPairedDevices() { 
    Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices(); 
    if (mPairedDevices.size() > 0) { 
     for (BluetoothDevice mDevice : mPairedDevices) { 
      Log.v(TAG, "PairedDevices: " + mDevice.getName() + " " + mDevice.getAddress()); 
     } 
    } 
} 

public void run() { 
    try { 
     mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(applicationUUID); 
     mBluetoothAdapter.cancelDiscovery(); 
     mBluetoothSocket.connect(); 
     mHandler.sendEmptyMessage(0); 
    } catch (IOException eConnectException) { 
     Log.d(TAG, "CouldNotConnectToSocket", eConnectException); 
     closeSocket(mBluetoothSocket); 
     return; 
    } 
} 

private void closeSocket(BluetoothSocket nOpenSocket) { 
    try { 
     nOpenSocket.close(); 
     Log.d(TAG, "SocketClosed"); 
    } catch (IOException ex) { 
     Log.d(TAG, "CouldNotCloseSocket"); 
    } 
} 

private Handler mHandler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     mBluetoothConnectProgressDialog.dismiss(); 
     Toast.makeText(PrintActivity.this, "DeviceConnected", Toast.LENGTH_SHORT).show(); 
    } 
    }; 
} 

DeviceListActivity.java

私は、この行の後に使用しているコードでは10
public class DeviceListActivity extends Activity { 

protected static final String TAG = "TAG"; 
private BluetoothAdapter mBluetoothAdapter; 
private ArrayAdapter<String> mPairedDevicesArrayAdapter; 
TextView txtName; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
    setContentView(R.layout.activity_device_list); 

    setResult(Activity.RESULT_CANCELED); 
    mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_raw,R.id.txtName); 

    ListView mPairedListView = (ListView) findViewById(R.id.paired_devices); 
    mPairedListView.setAdapter(mPairedDevicesArrayAdapter); 
    mPairedListView.setOnItemClickListener(mDeviceClickListener); 

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices(); 

    if (mPairedDevices.size() > 0) { 
     findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); 
     for (BluetoothDevice mDevice : mPairedDevices) { 
      mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" + mDevice.getAddress()); 
     } 
    } else { 
     String mNoDevices = getResources().getText(R.string.none_paired).toString(); 
     mPairedDevicesArrayAdapter.add(mNoDevices); 
    } 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    if (mBluetoothAdapter != null) { 
     mBluetoothAdapter.cancelDiscovery(); 
    } 
} 

private OnItemClickListener mDeviceClickListener = new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> mAdapterView, View mView, int mPosition, long mLong) { 
     mBluetoothAdapter.cancelDiscovery(); 
     String mDeviceInfo = ((TextView) mView).getText().toString(); 
     String mDeviceAddress = mDeviceInfo.substring(mDeviceInfo.length() - 15); 
     Log.v(TAG, "Device_Address " + mDeviceAddress); 

     Bundle mBundle = new Bundle(); 
     mBundle.putString("DeviceAddress", mDeviceAddress); 
     Intent mBackIntent = new Intent(); 
     mBackIntent.putExtras(mBundle); 
     setResult(Activity.RESULT_OK, mBackIntent); 
     finish(); 
    } 
    }; 
} 

答えて

0

は、あなたが、私はこのことができます願ってい

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
mBluetoothAdapter.startDiscovery();

が欠落している、私は唯一のアンドロイドのスタジオで始まるんだけど、誰も答えていないので、別のラインがありますこの質問は私が願っています。

+0

あなたは最高です – Peter

+0

助けてくれてうれしい! –

関連する問題