プリンターを検索する
通信したいプリンターを特定するための情報を取得します。 検索はメインスレッド外での実行をお勧めします。
サンプルコード
Wi-Fi プリンター
同一ネットワーク上の Wi-Fi 対応プリンターを検索します。
iOS - Objective-C:
- (BRLMPrinterSearchResult *)startSearchWiFiPrinter {
BRLMNetworkSearchOption *option = [[BRLMNetworkSearchOption alloc] init];
option.searchDuration = 15;
BRLMPrinterSearchResult * result;
result = [BRLMPrinterSearcher startNetworkSearch:option callback:^(BRLMChannel *channel){
NSString *modelName_ = [channel.extraInfo objectForKey:BRLMChannelExtraInfoKeyModelName];
NSString *ipaddress = channel.channelInfo;
NSLog(@"Model: %@, IP Address: %@", modelName, ipaddress);
}];
return result;
}
iOS - Swift:
fun startSearchWiFiPrinter() -> BRLMPrinterSearchResult {
let option = BRLMNetworkSearchOption()
option.searchDuration = 15
let result = BRLMPrinterSearcher.startNetworkSearch(option) { channel in
let modelName = channel.extraInfo?.value(forKey: BRLMChannelExtraInfoKeyModelName) as? String ?? ""
let ipaddress = channel.channelInfo
print("Model : \(modelName), IP Address: \(ipaddress)")
}
return result
}
Android - Java:
PrinterSearchResult startSearchWiFiPrinter()
{
NetworkSearchOption option = new NetworkSearchOption(15, false);
PrinterSearchResult result = PrinterSearcher.startNetworkSearch(context, option, new Consumer<Channel>() {
@Override
public void accept(Channel channel) {
String modelName = channel.getExtraInfo().get(Channel.ExtraInfoKey.ModelName);
String ipaddress = channel.getChannelInfo();
Log.d("TAG", "Model : $modelName, IP Address: $ipaddress")
}
});
return result
}
Android - Kotlin:
fun startSearchWiFiPrinter(): PrinterSearchResult
{
val option = NetworkSearchOption(15, false)
val result = PrinterSearcher.startNetworkSearch(context, option){ channel ->
val modelName = channel.extraInfo[Channel.ExtraInfoKey.ModelName] ?: ""
val ipaddress = channel.channelInfo
Log.d("TAG", "Model : $modelName, IP Address: $ipaddress")
}
return result
}
Bluetooth プリンター
ペアリング済みの Bluetooth 対応プリンターを検索します。
iOS - Objective-C:
- (NSArray<BRLMChannel *> *)startSearchBluetoothPrinter {
return [BRLMPrinterSearcher startBluetoothSearch].channels;
}
iOS - Swift:
func startSearchBluetoothPrinter() -> [BRLMChannel] {
return BRLMPrinterSearcher.startBluetoothSearch().channels
}
Android - Java:
ArrayList<Channel> startSearchBluetoothPrinter()
{
return PrinterSearcher.startBluetoothSearch(context).getChannels();
}
Android - Kotlin:
fun startSearchBluetoothPrinter(): ArrayList<Channel!>!
{
return PrinterSearcher.startBluetoothSearch(context).channels
}
Bluetooth Low Energy プリンター
モバイル端末の近くにある Bluetooth Low Energy 対応のプリンターを検索します。
iOS - Objective-C:
- (void)startSearchBLEPrinter {
BRLMBLESearchOption *option = [[BRLMBLESearchOption alloc] init];
option.searchDuration = 15;
BRLMPrinterSearchResult * result = [BRLMPrinterSearcher startBLESearch:option callback:^(BRLMChannel *channel){
NSString *modelName_ = [channel.extraInfo objectForKey:BRLMChannelExtraInfoKeyModelName];
NSString *advertiseLocalName = channel.channelInfo;
NSLog(@"Model: %@, AdvertiseLocalName: %@", modelName, advertiseLocalName);
}];
return result;
}
iOS - Swift:
func startSearchBLEPrinter() -> BRLMPrinterSearchResult {
let option = BRLMBLESearchOption()
option.searchDuration = 15
let result = BRLMPrinterSearcher.startBLESearch(option) { channel in
let modelName = channel.extraInfo?.value(forKey: BRLMChannelExtraInfoKeyModelName) as? String ?? ""
let advertiseLocalName = channel.channelInfo
print("Model : \(modelName), AdvertiseLocalName: \(advertiseLocalName)")
}
return result
}
Android - Java:
PrinterSearchResult startSearchBLEPrinter()
{
NetworkSearchOption option = new BLESearchOption(15);
PrinterSearchResult result = PrinterSearcher.startBLESearch(context, option, new Consumer<Channel>() {
@Override
public void accept(Channel channel) {
String modelName = channel.getExtraInfo().get(Channel.ExtraInfoKey.ModelName);
String localName = channel.getChannelInfo();
Log.d("TAG", "Model : $modelName, Local Name: $localName")
}
});
return result
}
Android - Kotlin:
fun startSearchBLEPrinter(): PrinterSearchResult
{
val option = BLESearchOption(15)
val result = PrinterSearcher.startBLESearch(context, option){ channel ->
val modelName = channel.extraInfo[Channel.ExtraInfoKey.ModelName] ?: ""
val localName = channel.channelInfo
Log.d("TAG", "Model : $modelName, Local Name: $localName")
}
return result
}
USB プリンター
USB 接続されたプリンターを検索します。
Android - Java:
ArrayList<Channel> startSearchUSBPrinter()
{
return PrinterSearcher.startUSBSearch(context).getChannels();
}
Android - Kotlin:
fun startSearchUSBPrinter(): ArrayList<Channel!>!
{
return PrinterSearcher.startUSBSearch(context).channels
}
備考
次の情報を別の方法で取得できる場合、この検索機能を利用する必要はありません。
OS | USB | Wi-Fi | Bluetooth | Bluetooth Low Energy |
---|---|---|---|---|
iOS | N/A | IP address | Serial Number | Local Name |
Android | No ID (*1) | IP address or MAC address | Bluetooth device address | Local Name |
- (*1) モバイル端末に接続済みであれば指定する必要はありません。